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

社会主义新天地 提交于 2019-12-01 22:30:16

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
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!