在嵌套片段中使用OptionsMenu [英] Working with OptionsMenu in nested fragment

查看:135
本文介绍了在嵌套片段中使用OptionsMenu的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用在我的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆