更改取决于片段的动作条菜单状态 [英] Changing the actionbar menu state depending on fragment

查看:114
本文介绍了更改取决于片段的动作条菜单状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想告诉我的行动吧,这取决于片段可见/隐藏的项目。

I am trying to show/hide items in my action bar depending on which fragment is visible.

在我MainActivity我有以下

In my MainActivity I have the following

/* Called whenever invalidateOptionsMenu() is called */
@Override
public boolean onPrepareOptionsMenu(Menu menu) {

    if(this.myFragment.isVisible()){
        menu.findItem(R.id.action_read).setVisible(true);
    }else{
        menu.findItem(R.id.action_read).setVisible(false);
    }

    return super.onPrepareOptionsMenu(menu);
} 

这伟大工程然而,当设备旋转存在问题。旋转结束的prepareOptionsMenu再次调用但是这个时候之后的 this.myFragment.isVisible()返回false ...,因此菜单项时隐时现明显的片段是可见的(如据最新显示在屏​​幕上)。

This works great however, when the device is rotated there is a issue. After the rotation is complete onPrepareOptionsMenu is called again however this time this.myFragment.isVisible() returns false...and hence the menu item is hidden when clearly the fragment is visible (as far as whats shown on the screen).

推荐答案

编辑:这是一个快速和肮脏的修复,请参见 es0329的答案的下面一个更好的解决方案。

This is a quick and dirty fix, see es0329's answer below for a better solution.

尝试添加该属性的活动在你的Andr​​oid清单标签:

Try adding this attribute to your activity tag in your android manifest:

android:configChanges="orientation|screenSize"

这篇关于更改取决于片段的动作条菜单状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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