Opening/Closing NavDrawer with invalidateOptionsMenu() cause onRestore call on SearchView and onQueryTextChange is called as well

后端 未结 1 603
孤城傲影
孤城傲影 2021-01-20 02:27

I have a MainActivity with callbacks implementation of:

  • DrawerListFragment.Callback
  • ItemListFragment.Callbacks
  • SearchView.OnQueryTextListener
相关标签:
1条回答
  • 2021-01-20 03:09

    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 of invalidateOptionsMenu().

    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
    }
    
    0 讨论(0)
提交回复
热议问题