使用 invalidateOptionsMenu() 打开/关闭 NavDrawer 会导致对 SearchView 和 onQueryTextChange 的 onRestore 调用也被调用

Posted

技术标签:

【中文标题】使用 invalidateOptionsMenu() 打开/关闭 NavDrawer 会导致对 SearchView 和 onQueryTextChange 的 onRestore 调用也被调用【英文标题】:Opening/Closing NavDrawer with invalidateOptionsMenu() cause onRestore call on SearchView and onQueryTextChange is called as well 【发布时间】:2014-10-30 14:43:10 【问题描述】:

我有一个带有回调实现的 MainActivity:

DrawerListFragment.Callback ItemListFragment.Callbacks SearchView.OnQueryTextListener

我还添加了一个ActionBar SearchView,最后实现了OnQueryTextListener 来改变listFragment 在每个kb 类型的listView,但这行得通!此外,单击 navDrawer 上的项目会为新类型的 itemsList 生成新的/替换 FragmentList 事务。

现在的问题。 ActionBar SearchView 实际上过滤了当前创建的 itemList。

使用经典的“new ActionBarDrawerToggle()...”实现打开/关闭 NavDrawer,并在覆盖方法中使用 invalidateOptionsMenu()。但是每次打开/关闭抽屉时,这个调用都会触发 onQueryTextChange 执行......查看日志! 我特意生成了一个异常来查看这些调用背后的堆栈跟踪:onRestore 实际上是为 SearchView 调用的,我的问题是为什么以及如何避免 SearchView 的这种自动触发?

我可能会检查 onQueryTextChange 是否打开抽屉以避免重新生成 itemsList。这可能是一个解决方案,但我想知道您的想法以及是否存在其他解决方案。

如果不清楚,我可能会上传一些代码。 日志在这里:

应用程序在开始时显示所有项目(抽屉位置 0)。

10-30 14:34:47.871: V/com.dav.pc.ui.MainActivity(900): MainActivity.initializeDrawer called!
10-30 14:34:47.880: D/com.dav.pc.ui.DrawerListFragment(900): New Drawer Instance!
10-30 14:34:47.900: D/com.dav.pc.ui.MainActivity(900): MainActivity.initializeActivity NewLaunch=Tutti gli elementi
10-30 14:34:47.920: D/com.dav.pc.ui.MainActivity(900): MainActivity.onDrawerItemSelected List selected: ALL
10-30 14:34:47.920: D/com.dav.pc.ui.MainActivity(900): MainActivity.onDrawerItemSelected Transaction Commit
10-30 14:34:47.920: D/com.dav.pc.ui.MainActivity(900): MainActivity.onDrawerItemSelected Label selected: ALL
10-30 14:34:47.930: V/com.dav.pc.ui.DrawerListFragment(900): DrawerListFragment.onCreate() called!
10-30 14:34:47.930: D/com.dav.pc.ui.DrawerListFragment(900): SetPosition onCreate activated_position=-1
10-30 14:34:47.980: V/com.dav.pc.ui.DrawerListFragment(900): DrawerListFragment.onViewCreated() called!
10-30 14:34:47.980: D/com.dav.pc.ui.DrawerListFragment(900): activated_position=0
10-30 14:34:47.980: V/com.dav.pc.ui.DrawerListFragment(900): DrawerListFragment.setSelection() called!
10-30 14:34:47.980: D/com.dav.pc.ui.DrawerListFragment(900): CurrentPosition=0
10-30 14:34:47.980: V/com.dav.pc.ui.DrawerListFragment(900): DrawerListFragment.onActivityCreated() called!
10-30 14:34:47.990: D/com.dav.pc.ui.ItemListFragment(900): ItemListFragment.onCreate Bundle exists
10-30 14:34:47.990: D/com.dav.pc.ui.ItemListFragment(900): Bundle[list_id=ALL]
10-30 14:34:47.990: D/com.dav.pc.ui.ItemListFragment(900): Create List ALL (16 items)
10-30 14:34:48.001: I/System.out(900): [mFirstLetters>Item 0,0]
10-30 14:34:48.001: I/System.out(900): [mFirstLetters>Item 1,1]
10-30 14:34:48.001: I/System.out(900): [mFirstLetters>Item 10,2]
10-30 14:34:48.001: I/System.out(900): [mFirstLetters>Item 11,3]
10-30 14:34:48.010: I/System.out(900): [mFirstLetters>Item 12,4]
10-30 14:34:48.010: I/System.out(900): [mFirstLetters>Item 13,5]
10-30 14:34:48.010: I/System.out(900): [mFirstLetters>Item 14,6]
10-30 14:34:48.020: I/System.out(900): [mFirstLetters>Item 15,7]
10-30 14:34:48.020: I/System.out(900): [mFirstLetters>Item 2,8]
10-30 14:34:48.020: I/System.out(900): [mFirstLetters>Item 3,9]
10-30 14:34:48.030: I/System.out(900): [mFirstLetters>Item 4,10]
10-30 14:34:48.040: I/System.out(900): [mFirstLetters>Item 5,11]
10-30 14:34:48.040: I/System.out(900): [mFirstLetters>Item 6,12]
10-30 14:34:48.051: I/System.out(900): [mFirstLetters>Item 7,13]
10-30 14:34:48.051: I/System.out(900): [mFirstLetters>Item 8,14]
10-30 14:34:48.051: I/System.out(900): [mFirstLetters>Item 9,15]
10-30 14:34:48.090: V/com.dav.pc.ui.MainActivity(900): onPostCreate called!
10-30 14:34:48.101: V/com.dav.pc.ui.ItemListFragment(900): ItemListFragment.onResume() called!
10-30 14:34:48.710: V/com.dav.pc.ui.MainActivity(900): MainActivity.onCreateOptionsMenu called!
I type 0 in the searchView:
10-30 14:34:55.421: V/com.dav.pc.ui.MainActivity(900): MainActivity.onQueryTextChange() newText: 0
10-30 14:34:55.440: I/System.out(900): [mFirstLetters>Item 0,0]
10-30 14:34:55.440: I/System.out(900): [mFirstLetters>Item 10,1]
this is another strange behaviour: getItemId in the ItemListAdapter is called 5 times... always, why?
10-30 14:34:55.470: D/com.dav.pc.ui.ItemListAdapter(900): getItemId position: 0
10-30 14:34:55.470: D/com.dav.pc.ui.ItemListAdapter(900): getItemId position: 0
10-30 14:34:55.490: D/com.dav.pc.ui.ItemListAdapter(900): getItemId position: 0
10-30 14:34:55.490: D/com.dav.pc.ui.ItemListAdapter(900): getItemId position: 0
10-30 14:34:55.540: D/com.dav.pc.ui.ItemListAdapter(900): getItemId position: 0
10-30 14:35:01.940: V/com.dav.pc.ui.MainActivity(900): MainActivity.onCreateOptionsMenu called!
onQueryTextChange is fired.
Here my controlled exception, method triggered when Drawer is opened!
This method creates a new ItemListAdapter and passed to the mItemListFragment:
10-30 14:35:02.080: V/com.dav.pc.ui.MainActivity(900): MainActivity.onQueryTextChange() newText: 0
10-30 14:35:02.090: I/System.out(900): [mFirstLetters>Item 0,0]
10-30 14:35:02.100: I/System.out(900): [mFirstLetters>Item 10,1]
10-30 14:35:01.980: W/System.err(900):  at com.dav.pc.ui.MainActivity.onQueryTextChange(MainActivity.java:306)
10-30 14:35:01.990: W/System.err(900):  at android.widget.SearchView.onTextChanged(SearchView.java:1081)
10-30 14:35:01.990: W/System.err(900):  at android.widget.SearchView.access$1900(SearchView.java:90)
10-30 14:35:01.990: W/System.err(900):  at android.widget.SearchView$10.onTextChanged(SearchView.java:1536)
10-30 14:35:01.990: W/System.err(900):  at android.widget.TextView.sendOnTextChanged(TextView.java:7577)
10-30 14:35:01.990: W/System.err(900):  at android.widget.TextView.setText(TextView.java:3259)
10-30 14:35:02.000: W/System.err(900):  at android.widget.TextView.setText(TextView.java:3110)
10-30 14:35:02.000: W/System.err(900):  at android.widget.EditText.setText(EditText.java:78)
10-30 14:35:02.000: W/System.err(900):  at android.widget.TextView.setText(TextView.java:3085)
10-30 14:35:02.000: W/System.err(900):  at android.widget.TextView.onRestoreInstanceState(TextView.java:2985)
10-30 14:35:02.000: W/System.err(900):  at android.view.View.dispatchRestoreInstanceState(View.java:9876)
10-30 14:35:02.010: W/System.err(900):  at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2330)
10-30 14:35:02.010: W/System.err(900):  at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2330)
10-30 14:35:02.010: W/System.err(900):  at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2330)
10-30 14:35:02.010: W/System.err(900):  at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2330)
10-30 14:35:02.020: W/System.err(900):  at android.view.View.restoreHierarchyState(View.java:9854)
10-30 14:35:02.020: W/System.err(900):  at com.android.internal.view.menu.MenuBuilder.restoreActionViewStates(MenuBuilder.java:358)
10-30 14:35:02.020: W/System.err(900):  at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:412)
10-30 14:35:02.020: W/System.err(900):  at com.android.internal.policy.impl.PhoneWindow.invalidatePanelMenu(PhoneWindow.java:739)
10-30 14:35:02.030: W/System.err(900):  at android.app.Activity.invalidateOptionsMenu(Activity.java:2552)
10-30 14:35:02.030: W/System.err(900):  at com.dav.pc.ui.MainActivity$1.onDrawerOpened(MainActivity.java:187)
10-30 14:35:02.030: W/System.err(900):  at android.support.v4.widget.DrawerLayout.dispatchOnDrawerOpened(DrawerLayout.java:488)
10-30 14:35:02.040: W/System.err(900):  at android.support.v4.widget.DrawerLayout.updateDrawerState(DrawerLayout.java:459)
10-30 14:35:02.040: W/System.err(900):  at android.support.v4.widget.DrawerLayout$ViewDragCallback.onViewDragStateChanged(DrawerLayout.java:1355)
...
10-30 14:35:42.040: V/com.dav.pc.ui.MainActivity(900): MainActivity.onCreateOptionsMenu called!
This is my controlled exception, method triggered when Drawer is closed.
Again the execution of the method.
10-30 14:35:42.170: V/com.dav.pc.ui.MainActivity(900): MainActivity.onQueryTextChange() newText: 0
10-30 14:35:42.170: I/System.out(900): [mFirstLetters>Item 0,0]
10-30 14:35:42.180: I/System.out(900): [mFirstLetters>Item 10,1]
10-30 14:35:42.080: W/System.err(900):  at com.dav.pc.ui.MainActivity.onQueryTextChange(MainActivity.java:306)
10-30 14:35:42.080: W/System.err(900):  at android.widget.SearchView.onTextChanged(SearchView.java:1081)
10-30 14:35:42.090: W/System.err(900):  at android.widget.SearchView.access$1900(SearchView.java:90)
10-30 14:35:42.090: W/System.err(900):  at android.widget.SearchView$10.onTextChanged(SearchView.java:1536)
10-30 14:35:42.090: W/System.err(900):  at android.widget.TextView.sendOnTextChanged(TextView.java:7577)
10-30 14:35:42.090: W/System.err(900):  at android.widget.TextView.setText(TextView.java:3259)
10-30 14:35:42.090: W/System.err(900):  at android.widget.TextView.setText(TextView.java:3110)
10-30 14:35:42.090: W/System.err(900):  at android.widget.EditText.setText(EditText.java:78)
10-30 14:35:42.100: W/System.err(900):  at android.widget.TextView.setText(TextView.java:3085)
10-30 14:35:42.100: W/System.err(900):  at android.widget.TextView.onRestoreInstanceState(TextView.java:2985)
10-30 14:35:42.100: W/System.err(900):  at android.view.View.dispatchRestoreInstanceState(View.java:9876)
10-30 14:35:42.110: W/System.err(900):  at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2330)
10-30 14:35:42.110: W/System.err(900):  at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2330)
10-30 14:35:42.110: W/System.err(900):  at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2330)
10-30 14:35:42.110: W/System.err(900):  at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2330)
10-30 14:35:42.120: W/System.err(900):  at android.view.View.restoreHierarchyState(View.java:9854)
10-30 14:35:42.120: W/System.err(900):  at com.android.internal.view.menu.MenuBuilder.restoreActionViewStates(MenuBuilder.java:358)
10-30 14:35:42.120: W/System.err(900):  at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:412)
10-30 14:35:42.120: W/System.err(900):  at com.android.internal.policy.impl.PhoneWindow.invalidatePanelMenu(PhoneWindow.java:739)
10-30 14:35:42.130: W/System.err(900):  at android.app.Activity.invalidateOptionsMenu(Activity.java:2552)
10-30 14:35:42.130: W/System.err(900):  at com.dav.pc.ui.MainActivity$1.onDrawerClosed(MainActivity.java:180)
10-30 14:35:42.140: W/System.err(900):  at android.support.v4.widget.DrawerLayout.dispatchOnDrawerClosed(DrawerLayout.java:477)
10-30 14:35:42.140: W/System.err(900):  at android.support.v4.widget.DrawerLayout.updateDrawerState(DrawerLayout.java:457)
10-30 14:35:42.140: W/System.err(900):  at android.support.v4.widget.DrawerLayout$ViewDragCallback.onViewDragStateChanged(DrawerLayout.java:1355)
....

=========================================================================
This is when instead I select a new ItemDrawer to generate the new list with fragment transaction:
10-30 14:37:47.961: D/com.dav.pc.ui.DrawerListAdapter(900): getItemId position: 5
10-30 14:37:48.100: D/com.dav.pc.ui.DrawerListAdapter(900): getItemId position: 5
10-30 14:37:48.100: D/com.dav.pc.ui.DrawerListAdapter(900): getItemId position: 0
10-30 14:37:48.100: D/com.dav.pc.ui.MainActivity(900): MainActivity.onDrawerItemSelected ListId selected: 4
10-30 14:37:48.100: D/com.dav.pc.ui.MainActivity(900): MainActivity.onDrawerItemSelected ClearSearch
10-30 14:37:48.161: V/com.dav.pc.ui.MainActivity(900): MainActivity.onQueryTextChange() newText: 
(I Should avoid this useless generation when clearing the searchText)
10-30 14:37:48.170: I/System.out(900): [mFirstLetters>Item 0,0]
10-30 14:37:48.170: I/System.out(900): [mFirstLetters>Item 1,1]
10-30 14:37:48.170: I/System.out(900): [mFirstLetters>Item 10,2]
10-30 14:37:48.170: I/System.out(900): [mFirstLetters>Item 11,3]
10-30 14:37:48.170: I/System.out(900): [mFirstLetters>Item 12,4]
10-30 14:37:48.181: I/System.out(900): [mFirstLetters>Item 13,5]
10-30 14:37:48.181: I/System.out(900): [mFirstLetters>Item 14,6]
10-30 14:37:48.181: I/System.out(900): [mFirstLetters>Item 15,7]
10-30 14:37:48.181: I/System.out(900): [mFirstLetters>Item 2,8]
10-30 14:37:48.181: I/System.out(900): [mFirstLetters>Item 3,9]
10-30 14:37:48.191: I/System.out(900): [mFirstLetters>Item 4,10]
10-30 14:37:48.191: I/System.out(900): [mFirstLetters>Item 5,11]
10-30 14:37:48.191: I/System.out(900): [mFirstLetters>Item 6,12]
10-30 14:37:48.191: I/System.out(900): [mFirstLetters>Item 7,13]
10-30 14:37:48.191: I/System.out(900): [mFirstLetters>Item 8,14]
10-30 14:37:48.191: I/System.out(900): [mFirstLetters>Item 9,15]
Again my controlled exception to see the stacktrace:
10-30 14:37:48.111: W/System.err(900):  at com.dav.pc.ui.MainActivity.onQueryTextChange(MainActivity.java:306)
10-30 14:37:48.111: W/System.err(900):  at android.widget.SearchView.onTextChanged(SearchView.java:1081)
10-30 14:37:48.111: W/System.err(900):  at android.widget.SearchView.access$1900(SearchView.java:90)
10-30 14:37:48.121: W/System.err(900):  at android.widget.SearchView$10.onTextChanged(SearchView.java:1536)
10-30 14:37:48.121: W/System.err(900):  at android.widget.TextView.sendOnTextChanged(TextView.java:7577)
10-30 14:37:48.121: W/System.err(900):  at android.widget.TextView.setText(TextView.java:3259)
10-30 14:37:48.121: W/System.err(900):  at android.widget.TextView.setText(TextView.java:3110)
10-30 14:37:48.121: W/System.err(900):  at android.widget.EditText.setText(EditText.java:78)
10-30 14:37:48.121: W/System.err(900):  at android.widget.TextView.setText(TextView.java:3085)
10-30 14:37:48.131: W/System.err(900):  at android.widget.SearchView.setQuery(SearchView.java:481)
10-30 14:37:48.131: W/System.err(900):  at com.dav.pc.ui.MainActivity.onDrawerItemSelected(MainActivity.java:210)
10-30 14:37:48.131: W/System.err(900):  at com.dav.pc.ui.DrawerListFragment.onListItemClick(DrawerListFragment.java:153)
10-30 14:37:48.131: W/System.err(900):  at android.app.ListFragment$2.onItemClick(ListFragment.java:160)
...
10-30 14:37:48.201: D/com.dav.pc.ui.MainActivity(900): MainActivity.onDrawerItemSelected Transaction Commit
10-30 14:37:48.201: D/com.dav.pc.ui.MainActivity(900): MainActivity.onDrawerItemSelected Label selected: 4
10-30 14:37:48.201: D/com.dav.pc.ui.MainActivity(900): MainActivity.onDrawerItemSelected ClosingDrawer
10-30 14:37:48.201: V/com.dav.pc.ui.DrawerListFragment(900): DrawerListFragment.setSelection() called!
10-30 14:37:48.211: D/com.dav.pc.ui.DrawerListFragment(900): CurrentPosition=5
10-30 14:37:48.211: D/com.dav.pc.ui.DrawerListAdapter(900): getItemId position: 0
10-30 14:37:48.211: D/com.dav.pc.ui.DrawerListAdapter(900): getItemId position: 5
10-30 14:37:48.411: V/com.dav.pc.ui.ItemListFragment(900): ItemListFragment.onPause() called!
10-30 14:37:48.431: D/com.dav.pc.ui.ItemListFragment(900): ItemListFragment.onCreate Bundle exists
10-30 14:37:48.431: D/com.dav.pc.ui.ItemListFragment(900): Bundle[list_id=4]
10-30 14:37:48.431: D/com.dav.pc.ui.ItemListFragment(900): Create List 4
(The list 4 contains 6 items, we are in onCreate)
10-30 14:37:48.431: I/System.out(900): [mFirstLetters>Item 0,0]
10-30 14:37:48.431: I/System.out(900): [mFirstLetters>Item 1,1]
10-30 14:37:48.431: I/System.out(900): [mFirstLetters>Item 2,2]
10-30 14:37:48.441: I/System.out(900): [mFirstLetters>Item 3,3]
10-30 14:37:48.441: I/System.out(900): [mFirstLetters>Item 4,4]
10-30 14:37:48.441: I/System.out(900): [mFirstLetters>Item 5,5]
10-30 14:37:48.461: V/com.dav.pc.ui.ItemListFragment(900): ItemListFragment.onResume() called!
10-30 14:37:48.721: V/com.dav.pc.ui.MainActivity(900): MainActivity.onCreateOptionsMenu called!
10-30 14:37:48.811: V/com.dav.pc.ui.MainActivity(900): MainActivity.onQueryTextChange() newText: 
10-30 14:37:48.811: I/System.out(900): [mFirstLetters>Item 0,0]
10-30 14:37:48.811: I/System.out(900): [mFirstLetters>Item 1,1]
10-30 14:37:48.821: I/System.out(900): [mFirstLetters>Item 2,2]
10-30 14:37:48.821: I/System.out(900): [mFirstLetters>Item 3,3]
10-30 14:37:48.821: I/System.out(900): [mFirstLetters>Item 4,4]
10-30 14:37:48.821: I/System.out(900): [mFirstLetters>Item 5,5]
10-30 14:37:48.761: W/System.err(900):  at com.dav.pc.ui.MainActivity.onQueryTextChange(MainActivity.java:306)
10-30 14:37:48.761: W/System.err(900):  at android.widget.SearchView.onTextChanged(SearchView.java:1081)
10-30 14:37:48.761: W/System.err(900):  at android.widget.SearchView.access$1900(SearchView.java:90)
10-30 14:37:48.761: W/System.err(900):  at android.widget.SearchView$10.onTextChanged(SearchView.java:1536)
10-30 14:37:48.761: W/System.err(900):  at android.widget.TextView.sendOnTextChanged(TextView.java:7577)
10-30 14:37:48.761: W/System.err(900):  at android.widget.TextView.setText(TextView.java:3259)
10-30 14:37:48.761: W/System.err(900):  at android.widget.TextView.setText(TextView.java:3110)
10-30 14:37:48.771: W/System.err(900):  at android.widget.EditText.setText(EditText.java:78)
10-30 14:37:48.771: W/System.err(900):  at android.widget.TextView.setText(TextView.java:3085)
10-30 14:37:48.771: W/System.err(900):  at android.widget.TextView.onRestoreInstanceState(TextView.java:2985)
10-30 14:37:48.771: W/System.err(900):  at android.view.View.dispatchRestoreInstanceState(View.java:9876)
10-30 14:37:48.771: W/System.err(900):  at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2330)
10-30 14:37:48.771: W/System.err(900):  at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2330)
10-30 14:37:48.781: W/System.err(900):  at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2330)
10-30 14:37:48.781: W/System.err(900):  at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2330)
10-30 14:37:48.781: W/System.err(900):  at android.view.View.restoreHierarchyState(View.java:9854)
10-30 14:37:48.781: W/System.err(900):  at com.android.internal.view.menu.MenuBuilder.restoreActionViewStates(MenuBuilder.java:358)
10-30 14:37:48.781: W/System.err(900):  at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:412)
10-30 14:37:48.781: W/System.err(900):  at com.android.internal.policy.impl.PhoneWindow.invalidatePanelMenu(PhoneWindow.java:739)
10-30 14:37:48.791: W/System.err(900):  at android.app.Activity.invalidateOptionsMenu(Activity.java:2552)
10-30 14:37:48.791: W/System.err(900):  at com.dav.pc.ui.MainActivity$1.onDrawerClosed(MainActivity.java:180)
...

【问题讨论】:

我在这里发布了我的解决方案:您可以查看***.com/a/27165274/1318946 @Patrik 我已经放了一个 if 来了解抽屉是否未打开 if (!mDrawerLayout.isDrawerOpen(mDrawerContainer) 以及插入的文本是否 not活动搜索文本(我将此信息保存在我的对象中)。比我重新生成列表。仍然不知道为什么打开/关闭抽屉会触发 onQueryTextChange。 【参考方案1】:

我刚刚找到了一个对我来说非常干净的新解决方案,也许其他人有兴趣:

在抽屉的打开/关闭实现中,我直接调用onPrepareOptionsMenu(mMenu) 而不是 invalidateOptionsMenu()

如您所见,我在创建时使用了保存在 mMenu 中的菜单实例。调用此方法可以避免使 searchView 无效,它仍然可能包含一些文本,因此不会被清除。

这里有另一个技巧(总是在 onPrepareOptionsMenu 内)来清除/图标化 searchView 我检查当前是否在我的服务类中设置了 searchText:

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

【讨论】:

如何检查 Searchview 中是否有文本?你如何获得一个处理程序到 searchview Textview/EditText/AutoCompleteTextview 来检查是否有任何文本? 嗨 Skynet,有一个接口可以实现并设置为 Widget:searchView.setOnQueryTextListener(...) 然后您实现 onQueryTextChange(String newText)onQueryTextSubmit(String query) 这是一种方便的方法,我正在使用它,但我似乎无法以编程方式更改 searchview 中的文本。我可以更改字体颜色,但不能更改文本! 你可以使用searchView.setQuery(newText, false); //true to submit it 我已经尝试了包括上述在内的一系列命令,第一次似乎没有任何效果,我必须更改选项卡两次才能清除搜索视图。谢谢伙计,我想我现在会深入研究一些开源代码,看看什么对我有用!

以上是关于使用 invalidateOptionsMenu() 打开/关闭 NavDrawer 会导致对 SearchView 和 onQueryTextChange 的 onRestore 调用也被调用的主要内容,如果未能解决你的问题,请参考以下文章

使用 invalidateOptionsMenu() 打开/关闭 NavDrawer 会导致对 SearchView 和 onQueryTextChange 的 onRestore 调用也被调用

invalidateOptionsMenu在片段中不起作用

Actionbar布局不好

Android场景-001- Menu动态改变

fragment 加载页面完成后如何刷新menu菜单

如何在操作栏中隐藏菜单项?