Proposed Alternative to implementing handling queries


#1

My proposal is to used the OnQueryTextListener() of the SearchView method instead of overriding onNewIntent.

In Listing 28.15, implement onQueryTextListener after setSearchableInfo. Now you can listen for queries when the user submits a search or while entering a search query. onQueryTextSubmit provides the same effect as Listing 28.9 onNewIntent.

[code]@Override
@TargetApi(11)
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater){
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.fragment_venue_search, menu);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
// Pull out the SearchView
MenuItem searchItem = menu.findItem(R.id.menu_item_search);
SearchView searchView = (SearchView)searchItem.getActionView();

        // Get the data from our searchable.xml as SearchableInfo
        SearchManager searchManager = (SearchManager)getActivity().getSystemService(Context.SEARCH_SERVICE);
        ComponentName name = getActivity().getComponentName();
        SearchableInfo searchInfo = searchManager.getSearchableInfo(name);
        searchView.setSearchableInfo(searchInfo);
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String s) {
                return true;
            }

            @Override
            public boolean onQueryTextChange(String s) {
                return true;
            }
        });
    }
}

[/code]