Skip to content Skip to sidebar Skip to footer

Opening/closing Navdrawer With Invalidateoptionsmenu() Cause Onrestore Call On Searchview And Onquerytextchange Is Called As Well

I have a MainActivity with callbacks implementation of: DrawerListFragment.Callback ItemListFragment.Callbacks SearchView.OnQueryTextListener I have also added an ActionBar Searc

Solution 1:

I just found a new solution that worked for me very clean, maybe someone else is interested:

In the open/close implementation of the Drawer I call directly onPrepareOptionsMenu(mMenu)instead ofinvalidateOptionsMenu().

As you see, I use the instance of the menu saved in mMenu at its creation. Calling this method I avoid to invalidate the searchView, that still could contain some text, therefore not to be cleared.

Here another trick (always inside onPrepareOptionsMenu) to clear/iconify the searchView I check if currently a searchText is previously set in my Service class:

if (!Service.getInstance().hasSearchText()) {
    Log.d(MainActivity.class.getSimpleName(), "onPrepareOptionsMenu Clearing SearchView!");
    SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
    searchView.setIconified(true);// This also clears the text in SearchView widget
}

Post a Comment for "Opening/closing Navdrawer With Invalidateoptionsmenu() Cause Onrestore Call On Searchview And Onquerytextchange Is Called As Well"