So I'm trying to perform a search using the app bar and passing the query to an attribute I call "searchRequest". I want to display the result.
I have been following this guide in the documentation and tried But then I to no avail.
As I have commented out, my temporary solution is to call my loader and make the "searchRequest" a class attribute. But this seems to cause multiple problems such as the result remaining on the screen despite pressing the back button.
I have tried the override the onOptionsMenuClosed()
method, setting an setOnCloseListener()
to my searchView.
I suspect I should be doing this in a dedicated SearchActivity.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
this.searchRequest = query;
/*LoaderManager loaderManager = getLoaderManager();
loaderManager.initLoader(BOOK_LOADER_ID, null,MainActivity.this);*/
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
}
Add this too, This will get called whenever search view expands or collapse.
MenuItemCompat.setOnActionExpandListener(searchView,
new MenuItemCompat.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem menuItem) {
return true;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem menuItem) {
// Refresh here with full list.
return true;
}
});