Reputation: 9301
I have defined my SearchView
like this
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater)
SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
SearchableInfo info = searchManager.getSearchableInfo(getActivity().getComponentName());
SearchView searchView = (SearchView) menu.findItem(R.id.action_menu_search).getActionView();
searchView.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
searchView.setSearchableInfo(info);
}
Everything works fine. When I submit query new intent is fired and I capture it in my MainActivity
. But I do not know where the search query is coming from.
I can see mAppSearchData
variable that could help me in android.widget.SearchView
, but it is not accessible for some reason - code searchView.setAppSearchData(bundle);
does not compile.
Is there some other way to pass pass additional data to detect where the search is coming from?
Upvotes: 3
Views: 993
Reputation: 1090
You can implement your own OnQueryTextListener e.g.:
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
Intent intent = new Intent(getApplicationContext(), MyActivity.class);
intent.putExtra(MyActivity.IMPORTANT_NUMBER, importantNumber);
intent.putExtra(SearchManager.QUERY, s);
intent.setAction(Intent.ACTION_SEARCH);
startActivity(intent);
return true;
}
@Override
public boolean onQueryTextChange(String s) {
return false;
}
});
More Details: https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/widget/SearchView.java
It is important that you return true in onQueryTextSubmit, so the searchView does not perform the default action.
Upvotes: 1
Reputation: 549
You could send the activity name with the query by appending the query. Let's say your query is 'hello', you would append it to something like that 'mainactivity%%hello'. In your search activity, you can then parse the query to retrieve the activity name. Consider this solution has a workaround. There's probably something better out there.
Upvotes: 0