[英]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.