简体   繁体   中英

Hide Menu item in a Fragment on button click Android

I have a fragment where I am displaying a Menu with 2 menu items. Currently the menu items are shown as the fragment is displayed. Now, I want to hide them once a certain action takes place eg. User deletes some text. I have tried the following code but the menu items still display :

 @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
    {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.menu_details, menu);
    }

    @Override
    public void onPrepareOptionsMenu(Menu menu)
    {
            if (isDeleted)
            {
                status = false;
                menu.findItem(R.id.action_edit).setVisible(false);
            }
        }
    }

How can I hide and unhide the menu items inside a fragment?

What you've got now seems good, you're just missing invalidateOptionsMenu call:

isDeleted = true;
getActivity().invalidateOptionsMenu();

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