简体   繁体   中英

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. I have the menu item in the onCreateOptionsMenu and based on the current directory that is open, it alters the visibility. 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. 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.

I hope this helps. Please let me know if you need more details or help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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