简体   繁体   English

Android:onCreateOptionsMenu在一个片段中膨胀所有其他片段中的菜单

[英]Android: onCreateOptionsMenu in one fragment inflates it's menu in all other fragments

To begin with, I have a fragment-based app. 首先,我有一个基于片段的应用程序。 On the first fragment user sees recyclerview and I'm calling onCreateOptionsMenu() which makes menu with a searchview . 在第一个片段上,用户看到了recyclerview ,我正在调用onCreateOptionsMenu() ,它使用searchview创建菜单。 The problem is, when I switch to other fragments I also see this menu. 问题是,当我切换到其他片段时,我也看到了这个菜单。 And if another fragment has its own menu, inflater adds items from both menus. 如果另一个片段有自己的菜单, inflater会从两个菜单中添加项目。 As a result, I see my searchview -icon everywhere. 结果,我到处都看到了我的searchview -icon。 Can you please explain me, what am I doing wrong? 你能解释一下我,我做错了什么?

Try to override onCreateOptionsMenu() in all your fragments and call super.onCreateOptionsMenu() before anything else. 尝试override所有片段中的 onCreateOptionsMenu() ,然后再调用 super.onCreateOptionsMenu() Alternatively , use menu.clear() in those other fragments. 或者 ,在其他片段中使用menu.clear()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 菜单抽屉即使没有显示,碎片也会使菜单膨胀 - Menu drawer Fragments inflates their menu even when not shown Android NavigationDrawerFragment:控制是否调用子片段或父活动的onCreateOptionsMenu,但不能同时调用 - Android NavigationDrawerFragment: Control whether the child fragment or parent activity's onCreateOptionsMenu gets called, but not both 滑块菜单的Android片段,其中的选项卡内部有片段 - Android Fragment for Slider Menu which has Fragments inside for Tabs Android将一个片段导航到另一个片段可以正常工作,但无法看到其他片段的背景 - Android navigate one fragment to another is working but , cannot see other fragment's background Android:如果未使用多个片段,则在活动的片段中进行布局 - Android: making layouts in the activity's fragment, if not using several fragments Android选项卡式活动:如何创建片段以扩大活动布局并在片段上运行活动? - Android Tabbed Activity: How to create a fragment that inflates a activity layout and runs a activity on the fragment? onCreateOptionsMenu() 在 Fragment 中不起作用 - onCreateOptionsMenu() does not work in Fragment 选项卡式菜单的Android片段 - Android Fragments for tabbed menu 为错误的片段调用OnCreateOptionsMenu - OnCreateOptionsMenu is called for the wrong Fragment 保持片段同步-Fragment Android - Keep Fragments Synconized - Fragment Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM