Skip to content Skip to sidebar Skip to footer

Changing Visibility Of Menu Items In Fragment

I'm trying to hide some menu items when a fragment is changed, but seems that this is not working. Here is what im doing: Defining the menu and menu items: @Override public boo

Solution 1:

I don't know why it does not work, but try to change the visibility directly in the onCreateOptionMenu.

If it works, then when you want to hide it call invalidateOptionsMenu (or supportInvalidateOptionsMenu for actionbar compat), it will force the onCreateOptionMenu to be called again and you can update the menu if the fragment is there or not.

Solution 2:

in your fragment add below code

@OverridepublicvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

@OverridepublicvoidonCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
    }



 @OverridepublicvoidonPrepareOptionsMenu(Menu menu) {
        super.onPrepareOptionsMenu(menu);
        menu.findItem(R.id.action_cart).setVisible(false);
        menu.findItem(R.id.action_search).setVisible(false);
        menu.findItem(R.id.overflow).setVisible(false);
    }

Post a Comment for "Changing Visibility Of Menu Items In Fragment"