Jim
Jim

Reputation: 19562

How can I change a menu from a fragment?

In a fragment I see in the onCreate setHasOptionsMenu(true);
I see the menu.
But I can't understand, if I need to change something in the menu where should I do that programmatically?
Now I see a menu which I am not sure which resource file is loaded but how can I change/update the menu before it is actually rendered e.g. to remove an item?

Update: I don't need todo this from the fragment. The hosting activity is fine but I don't know where should I do this and how

Upvotes: 0

Views: 111

Answers (2)

ianhanniballake
ianhanniballake

Reputation: 199825

You can override onPrepareOptionsMenu(), which is called before the menu is displayed and everytime you call FragmentActivity.supportInvalidateOptionsMenu(). There you can call setVisible() to hide or show menu items as needed.

Upvotes: 2

Marcin Orlowski
Marcin Orlowski

Reputation: 75629

I need to change something in the menu where should I do that programmatically?

While menu is not managed by your fragment, I'd suggest to make fragment calling host Activity's method of your choice to request the change, but the change itself should be done by Activity

Upvotes: 0

Related Questions