Using onPrepareOptionsMenu instead of onCreateOptionsMenu in Fragment

23,929

You should call super.onCreateOptionsMenu(menu, inflater); after you have created your menu, not before. That sends the menu up in the hierarchy and other fragments or the activity may want to add items themselves.

The activity is responsible for the displaying and managing the menu, so if you change the visibility after it has been sent to the activity, nothing much is going to happen.

Now, when you call super.onPrepareOptionsMenu(menu); it will "prepare" it's menu, but it will now take the changes you've made in the onCreateOptionsMenu into account.

Share:
23,929
Sebastian Contreras
Author by

Sebastian Contreras

Updated on December 06, 2020

Comments

  • Sebastian Contreras
    Sebastian Contreras over 3 years

    I had a problem setting up some fragment menu items in the ActionBar and I found a way to solve it, but I don't understand why it worked.

    I wanted to change the visibility in a menu item right after I inflated it from a menu xml file in onCreateOptionsMenu method. The code seems to work fine, but there's no visible effect. I solved the problem inflating the menu in onCreateOptionsMenu method but changing the visibility of it in onPrepareOptionsMenu method.

    What I want to know is why changing the visibility in onCreateOptionsMenu does not work.

    What can I do in onPrepareOptionsMenu that I can't do in onCreateOptionsMenu?

    Is there any pattern to follow here?

    Thanks!

    Here's the relevant code, just in case:

    public class MyFragment extends Fragment {
    
        @Override
        public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
            super.onCreateOptionsMenu(menu, inflater);
            inflater.inflate(R.menu.my_menu, menu);
    
            // This does not work, compiles and runs fine, but has no visible effect
            MenuItem someMenuItem = menu.findItem(R.id.some_menu_item);
            someMenuItem.setVisible(false);
        }
    
        @Override
        public void onPrepareOptionsMenu(Menu menu) {
            super.onPrepareOptionsMenu(menu);
    
            // This does work
            MenuItem someMenuItem = menu.findItem(R.id.some_menu_item);
            someMenuItem.setVisible(false);
        }
    }