Getting Android to call onActivityResult() after onSearchRequested()

Because everything is handled through the SearchManager in Android, onActivityResult() is not typically called after onSearchRequested().

What I was looking for was a clean way to get search results back to the activity that originally called onSearchRequested().

I first tried the most obvious approach of calling setResult() and finish() from the search result (searchable) activity, but because the activity isn’t launched using startActivityForResult() it doesn’t work. (Complicating things further, the SearchManager is the one launching the activity so it really doesn’t even work in theory).

Solution: I had to make the original calling activity also the searchable activity, so my entry in the manifest looks like this:

   <activity android:name=".MyBaseActivity"
             android:launchMode="singleTop">

      <!-- MyBaseActivity is also the searchable activity -->
      <intent-filter>
         <action android:name="android.intent.action.SEARCH" />
      </intent-filter>
      <meta-data android:name="android.app.searchable"
                 android:resource="@xml/searchable"/>

      <!-- enable the base activity to send searches to itself -->
      <meta-data android:name="android.app.default_searchable"
                 android:value=".MyBaseActivity" />
   </activity>

Then, as described in the android documentation, I created a handleActivity method in which I manually start the search activity for result:

   private void handleIntent(Intent intent) {
      // Get the intent, verify the action and get the query
      if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        String query = intent.getStringExtra(SearchManager.QUERY);
        // manually launch the real search activity
        final Intent searchIntent = new Intent(getApplicationContext(),
              MySearchActivity.class);
        // add query to the Intent Extras
        searchIntent.putExtra(SearchManager.QUERY, query);
        startActivityForResult(searchIntent, ACTIVITY_REQUEST_CODE);
      }

From there it is just a matter of having the search activity perform its search as normal and, when a result is clicked, call setResult and finish. The original activity will then get the result as expected in the MyBaseActivity.onActivityResult method.


Comments

  1. Thanks dude for nice workaround :)
    It can be done by dirty coding however this way is much better and cleaner.

    Reply
    • Glad you found it useful. You’d think there’d be a more obvious way to do this, but I don’t think anything has changed to make this easier.

      Reply
  2. Fernando Piedra
    July 1, 2015 - 3:43 pm

    Thanks buddy, this is very useful.

    Reply
  3. Hi Dave,

    Can you help me out. I’ve tried using your work-around but on making my MainActivity the Searchable activity as well, the SearchView widget is not being called. Any pointers as to how I can get past this.

    I’ve added the SearchView by following the information on the here: http://developer.android.com/guide/topics/search/search-dialog.html#ReceivingTheQuery.

    Thanks in advance for any pointers.

    Reply
    • Hi Tinashe. Honestly, it’s been over 4 years since I first wrote this post so it’s not all fresh in my mind. I think the SearchView would go in what I call the MySearchActivity activity/class in the example above and is manually launched with startActivityForResult

      Reply

Leave a Reply

Your email address will not be published / Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>