简体   繁体   English

Android-重新创建主要活动后菜单项未显示

[英]Android - Menu Item does not show up after recreating Main Activity

I am having issues with menu items in the action bar. 我在操作栏中的菜单项有问题。 It seems like when I set the visibility to an item to false and then recreate the activity, it doesn't show up anymore. 似乎当我将某个项目的可见性设置为false然后重新创建该活动时,该活动不再显示。 I have the menu item in the onCreateOptionsMenu and based on the current directory that is open, it alters the visibility. 我在onCreateOptionsMenu中具有菜单项,并且基于打开的当前目录,它会更改可见性。 It works fine, until I recreate the activity. 在我重新创建活动之前,它工作正常。 Then it doesn't show up at all. 然后它根本不显示。 It stays invisible.. Is there a way to manually recreate the menu too so it defaults back to what is in the menu file? 它保持不可见。.是否也有办法手动重新创建菜单,因此它默认返回到菜单文件中的内容?

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        if (currentDirectory.equals("/sdcard"))
            menu.findItem(R.id.itemOne).setVisible(false);
        return true;

    }

I think what you need here is the invalidateOptionsMenu() method. 我认为您这里需要的是invalidateOptionsMenu()方法。 What this does is that it forces the menu to be recreated. 这是因为它强制重新创建菜单。

What you do is call this method when you want the menu to return to default and then in your activity, you override public boolean onPrepareOptionsMenu(Menu menu) and make the changes you want to occur right after the menu has been recreated. 您要做的是在希望菜单恢复为默认值时调用此方法,然后在您的活动中,重写public boolean onPrepareOptionsMenu(Menu menu)并在重新创建菜单后立即进行更改。

I hope this helps. 我希望这有帮助。 Please let me know if you need more details or help. 如果您需要更多详细信息或帮助,请告诉我。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM