According to the official documentation, there are two ways of providing a search interface: using either the search dialog or a SearchView
widget. I'd like to pay attention to passing search context data using these two ways.
So, the documentation says:
..you can provide additional data in the intent that the system sends to your searchable activity. You can pass the additional data in the APP_DATA Bundle, which is included in the ACTION_SEARCH intent.
To pass this kind of data to your searchable activity, override the onSearchRequested() method for the activity from which the user can perform a search, create a Bundle with the additional data, and call startSearch() to activate the search dialog. For example:
@Override public boolean onSearchRequested() { Bundle appData = new Bundle(); appData.putBoolean(SearchableActivity.JARGON, true); startSearch(null, false, appData, false); return true; }
..Once the user submits a query, it's delivered to your searchable activity along with the data you've added. You can extract the extra data from the APP_DATA Bundle to refine the search. For example:
Bundle appData = getIntent().getBundleExtra(SearchManager.APP_DATA); if (appData != null) { boolean jargon = appData.getBoolean(SearchableActivity.JARGON); }
This refers to the search dialog. And what about the search widget?
Is it possible to pass search context data using the SearchView
widget only?
Hope, someone could give clear explanation and/or suggest another or similar way to accomplish the goal.
Thanks!
I've discovered the solution. Even two solutions!
They don't need to invoke
onSearchRequested()
thus there is no search dialog at all :)First, I provide some common steps to create the search interface and then give the solutions of the source problem.
We add the Search View to the App Bar by creating
res/menu/options_menu.xml
file with the following code:Create a Searchable Configuration in
res/xml/searchable.xml
file:Declare two activities in
AndroidManifest.xml
:Create a Searchable Activity where we handle the
ACTION_SEARCH
intent and the search context data, passed from theMainActivity
. We extract the extra data from theAPP_DATA
Bundle
to refine the search:Now, we need to implement our
MainActivity
class. So, we inflate our menu and configure theSearchView
element as well. We also need to set theSearchView.OnQueryTextListener
and implement its methods, especiallyonQueryTextSubmit()
. It is invoked when the user presses the submit button and it contains the main logic of passing the search context data to theSearchableActivity
. Finally, we reached the main answer section. As I said, there are two solutions:1. Create an intent with
Bundle
extra and send it to theSearchableActivity
manually;Here is the
MainActivity
with all necessary contents:Thanks to https://stackoverflow.com/a/22184137/6411150.
The second solution is also put in
onQueryTextSubmit()
(but it's not necessary):2. Create search context data
Bundle
and pass it to thesetAppSearchData()
method of theSearchView
.So, we don't need to create and pass the whole search intent and launch respective searchable activity, the system will take care of it.
Here is another code snippet:
Thanks to https://stackoverflow.com/a/38295904/6411150.
Note: Only support library's version of
SearchView
(android.support.v7.widget.SearchView
) contains thesetAppSearchData()
method, so be attentive.If the design works for your needs, you can use the SearchView by itself, and add an OnQueryTextListener to it, and deal with it there. There is no need for anything else, no Intents, Meta-tags, nor XML files. I have done this a few times, and the docs are a bit not clear on this.