13

I am successfully using a search widget in my action bar to perform a search following this guide. The search is fine, but I'm wondering how to pass additional variables on a search. The same guide states I can override onSearchRequested(), but this doesn't seem to work with a search widget.

  • Override in question:

    @Override
    public boolean onSearchRequested() {    
        Bundle appData = new Bundle();
        appData.putString("KEY", "VALUE");
        startSearch(null, false, appData, false);
        return true;
    }
    
  • Getting the bundle in my activity class:

    protected void onCreate(Bundle savedInstanceState) {
        // ...
        Intent intent = getIntent();
        Bundle appData = intent.getBundleExtra(SearchManager.APP_DATA);
        String value = appData.getString("KEY");
        Log.d("VALUE", value);
        // ...
    }
    

My application crashes upon creating the search class because appData is always null.

Note

onSearchRequested() is called, but the bundle does not make it to my onCreate() method.

All extras from the passed intent are {user_query=my-query, query=my-query}.

Charles
  • 50,943
  • 13
  • 104
  • 142
danada
  • 564
  • 5
  • 13

3 Answers3

24

It seems the only way to do this is to intercept new activities created in your activity which is search-enabled. To do this we override the startActivity() method. We can then check to make sure the activity is indeed the search activity, then add an extra to the intent. The working code is below.

@Override
public void startActivity(Intent intent) {      
    // check if search intent
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        intent.putExtra("KEY", "VALUE");
    }

    super.startActivity(intent);
}

You can then grab your extra as you would any other extra in your search activity using:

mValue = intent.getStringExtra("KEY");
danada
  • 564
  • 5
  • 13
  • Thanks. It works great. Btw [this issue](http://code.google.com/p/android/issues/detail?id=15579) exists in API 11 only (I tested on emulators). –  Mar 28 '13 at 16:24
  • just use it on your activity class. fragment has startActivity function but it doesn't work for this situation. – okarakose Aug 23 '16 at 21:45
  • This answer is wrong!! It is not working at all. See penduDevs answer. And see https://developer.android.com/guide/topics/search/search-dialog.html Section: "Passing search context data" – Ton Snoei Nov 16 '17 at 15:32
  • @Ton Snoei if you cant make it work, it is your problem. it is working for me. But yeah, truth that it can be done also in other ways! – Andris Feb 13 '19 at 10:34
1

You can override the onSearchRequested method inside the Activity that's invoking the search.

@Override
public boolean onSearchRequested() {
     Bundle appData = new Bundle();
     appData.putBoolean(SearchableActivity.JARGON, true);
     startSearch(null, false, appData, false);
     return true;
 }

then you can extract this data inside the SearchableActivity

Bundle appData = getIntent().getBundleExtra(SearchManager.APP_DATA);
if (appData != null) {
    boolean jargon = appData.getBoolean(SearchableActivity.JARGON);
}
penduDev
  • 4,743
  • 35
  • 37
0

I think you want to just use

String value = intent.getStringExtra(SearchManager.APP_DATA);

because the intent holds the bundle you passed to start the search.

harmanjd
  • 1,874
  • 19
  • 21
  • Thanks for the reply! Unfortunately, this does not work. The `SearchManager.APP_DATA` key is supposed to be used while sending a _bundle_ and not _string_. – danada Jan 30 '13 at 16:01
  • Oh I see. Have you looked at the extras on the intent (intent.getExtras) using the debugger to see if anything is set there? I was under the impression that whatever you put into the bundle in the startSearch call would be available as the extras on the intent in the search activity. – harmanjd Jan 30 '13 at 16:41
  • documentation does indeed indicate that your approach should work. I don't see any indication that this has changed across api versions, but I would still suggest checking the extras on the intent just in case. – harmanjd Jan 30 '13 at 17:02
  • The only data I could pull from the intent are the action `android.intent.action.SEARCH` and the query `{user_query=my query, query=my query}` – danada Jan 30 '13 at 17:40
  • I've found someone else saying the bundle won't get send [here](http://stackoverflow.com/questions/6372557/problem-passing-a-bundle-with-onsearchrequested), but without explanation. The alternative offered there doesn't let me add additional variables. – danada Jan 30 '13 at 17:47
  • Aha, I figured it out. Will be posting an answer soon q: – danada Feb 02 '13 at 05:55