在嵌套片段中使用OptionsMenu [英] Working with OptionsMenu in nested fragment
问题描述
我使用在我的hostactivity MenuActivity 中实现的NavigationDrawer模式。我的导航有3项:项目1,项目2,项目3.
每个项目都绑定到一个片段。
当我点击项目1时,一个片段A,它实现了一个带有多个片段(嵌套片段)的ViewPager。
在我的嵌套片段中,我使用下面的方法(它工作正常!
@Override
public void onCreateOptionsMenu(Menu Menu,MenuInflater inflater){
inflater.inflate(R .menu.my_menu,菜单);
super.onCreateOptionsMenu(menu,inflater);
}
但是当我点击我的菜单中的另一个元素时(Item 2 - > display片段B或项目3->显示片段C),我的菜单(在我的嵌套片段中膨胀)始终可见,但我希望它消失。
你有解决这个问题的办法吗?
添加 setRetainInstance(true)
和 setHasOptionMenu(true)
位于 onCreate()
中。
I use a NavigationDrawer pattern that is implemented in my hostactivity MenuActivity. My navigation has 3 items: Item 1, Item 2, Item 3. Each itemis bonded to a fragment.
When I click on Item 1, I displayed a fragment A that implements a ViewPager with several fragments (nested fragments).
In my nested fragments, I inflate a menu with the following method (It works fine !) :
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.my_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
But when I click on another element of my menu (Item 2 -> display Fragment B or Item 3->display Fragment C), my menu (which was inflated in my nested fragment) is always visible but I want it to disappear.
Would you have a solution to this problem? Thank you in advance.
Add setRetainInstance(true)
and setHasOptionMenu(true)
in onCreate()
in fragment.
这篇关于在嵌套片段中使用OptionsMenu的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!