47

I have been searching a lot on invalidateOptionsMenu() and I know what it does. But I cannot think of any real life example where this method could be useful.

I mean, for instance, let's say we want to add a new MenuItem to our ActionBar, we can simply get the Menu from onCreateOptionsMenu(Menu menu) and use it in any button's action.

Now to my real question, is following the only way of using invalidateOptionsMenu()?

bool _OtherMenu;
protected override void OnCreate (Bundle bundle)
{
    _OtherMenu = false;
    base.OnCreate (bundle);
    SetContentView (Resource.Layout.Main);
    Button button = FindViewById<Button> (Resource.Id.myButton);
    button.Click += delegate
    {
        if(_OtherMenu)
            _OtherMenu = false;
        else
            _OtherMenu = true;

        InvalidateOptionsMenu ();
    };
}

public override bool OnCreateOptionsMenu (IMenu menu)
{
    var inflater = this.SupportMenuInflater;
    if(_OtherMenu)
        inflater.Inflate (Resource.Menu.another_menu, menu);
    else
        inflater.Inflate (Resource.Menu.main_activity_menu, menu);

    return base.OnCreateOptionsMenu (menu);
}

Click the button and a different menu appears. Click the button again and previous menu appears.

P.S. Sorry for the C# syntax.

Bugs Happen
  • 2,169
  • 4
  • 33
  • 59

9 Answers9

82

invalidateOptionsMenu() is used to say Android, that contents of menu have changed, and menu should be redrawn. For example, you click a button which adds another menu item at runtime, or hides menu items group. In this case you should call invalidateOptionsMenu(), so that the system could redraw it on UI. This method is a signal for OS to call onPrepareOptionsMenu(), where you implement necessary menu manipulations. Furthermore, OnCreateOptionsMenu() is called only once during activity (fragment) creation, thus runtime menu changes cannot be handled by this method.

All can be found in documentation:

After the system calls onCreateOptionsMenu(), it retains an instance of the Menu you populate and will not call onCreateOptionsMenu() again unless the menu is invalidated for some reason. However, you should use onCreateOptionsMenu() only to create the initial menu state and not to make changes during the activity lifecycle.

If you want to modify the options menu based on events that occur during the activity lifecycle, you can do so in the onPrepareOptionsMenu() method. This method passes you the Menu object as it currently exists so you can modify it, such as add, remove, or disable items. (Fragments also provide an onPrepareOptionsMenu() callback.)

On Android 2.3.x and lower, the system calls onPrepareOptionsMenu() each time the user opens the options menu (presses the Menu button).

On Android 3.0 and higher, the options menu is considered to always be open when menu items are presented in the action bar. When an event occurs and you want to perform a menu update, you must call invalidateOptionsMenu() to request that the system call onPrepareOptionsMenu().

Alexander Zhak
  • 9,140
  • 4
  • 46
  • 72
  • but i dont have to call invalidateOptionsMenu(). I simply do menu.add() and it is shown – Bugs Happen Jan 16 '15 at 13:11
  • It's needed for dynamic menus. How could you call `menu.add()` from outside `onCreateOptionsMenu()`? How could you make, e.g., menu items change when user scrolls down a listview? By invalidating the menu on scroll, forcing the activity to recreate it, and checking for a certain condition within `onCreateOptionsMenu()`. This wouldn't work if you check for the condition within `onCreateOptionsMenu()` without invalidating, because the activity would think the menu is still OK. – natario Jan 16 '15 at 13:40
  • i don't know how to write code here but lets see: OnCreateOptionsMenu(IMenu menu){ _Menu = menu; } And you have the reference to Menu. Now do whatever you want to do – Bugs Happen Jan 16 '15 at 14:06
25

use this to reload new menu during app lifecycle:

new:

getActivity().invalidateOptionsMenu();

old

ActivityCompat.invalidateOptionsMenu(getActivity());
itzhar
  • 12,743
  • 6
  • 56
  • 63
3

You need to override method onPrepareOptionsMenu(), write your update code of action menu in same method and if you are using fragment then add setHasOptionsMenu(true); in onCreateView().

Hope it helps you

Vasily Kabunov
  • 6,511
  • 13
  • 49
  • 53
Richa
  • 3,165
  • 1
  • 22
  • 26
3

One use I've found is forcing an order of operations between onResume and onCreateOptionsMenu/onPrepareOptionsMenu. The natural order (as of platform 22 at least) seems to flip flop around, especially when re-orientating your device.

Call invalidateOptionsMenu() in onResume() and you'll guarantee that onPrepareOptionsMenu will be called after onResume (it may additionally be called before). For example, this will allow enabling a menu item based on data retrieved in onResume.

Goose
  • 101
  • 4
1
/**
 * Set a hint for whether this fragment's menu should be visible.  This
 * is useful if you know that a fragment has been placed in your view
 * hierarchy so that the user can not currently seen it, so any menu items
 * it has should also not be shown.
 *
 * @param menuVisible The default is true, meaning the fragment's menu will
 * be shown as usual.  If false, the user will not see the menu.
 */
public void setMenuVisibility(boolean menuVisible) {
    if (mMenuVisible != menuVisible) {
        mMenuVisible = menuVisible;
        if (mHasMenu && isAdded() && !isHidden()) {
            mHost.onSupportInvalidateOptionsMenu();
        }
    }
}

XML menu sample:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
    android:id="@+id/action_edit"
    android:icon="@drawable/ic_edit"
    android:title="Edit Task"
    app:showAsAction="always" />

<item
    android:id="@+id/action_delete"
    android:icon="@drawable/ic_delete"
    android:title="Delete Task"
    app:showAsAction="always" />

<item
    android:id="@+id/action_check"
    android:icon="@drawable/ic_check"
    android:title="Check Task"
    app:showAsAction="always" />

<item
    android:id="@+id/action_uncheck"
    android:icon="@drawable/ic_undo"
    android:title="Uncheck Task"
    app:showAsAction="always" />
</menu>

Code inside a sample fragment:

private boolean isMenuItemChecked;

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    setMenuVisibility(false);
}

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

@Override
public void onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);
    try {
        menu.findItem(R.id.action_check).setVisible(!isMenuItemChecked);
        menu.findItem(R.id.action_uncheck).setVisible(isMenuItemChecked);
    }
    catch(Exception e) {
        Log.e(TAG, "onPrepareOptionsMenu error");
    }
}

public void loadUi(boolean isMenuItemChecked) {
    this.isMenuItemChecked = isMenuItemChecked;
    setMenuVisibility(true);
}
1
  1. Put the initial state of the menu in onCreateOptionsMenu(...).

  2. Use the invalidateOptionsMenu() to force onCreateOptionsMenu(...) and onPrepareOptionsMenu(...).

  3. In onPrepareOptionsMenu(...), call menu.clear() to remove all items from the menu.

  4. Still in onPrepareOptionsMenu(...) place your dynamic menu changes after the clear.

double-beep
  • 5,031
  • 17
  • 33
  • 41
0

Edit: Here is a better answer to the question.

A good use for invalidateOptionsMenu() is when we have a ListView and Delete All MenuItem so when the ListView is empty we should use invalidateOptionsMenu() to remove the Delete All MenuItem.

Here is a question related to this answer: Question.

Community
  • 1
  • 1
  • If you have a new question, please ask it by clicking the [Ask Question](http://stackoverflow.com/questions/ask) button. Include a link to this question if it helps provide context. - [From Review](/review/low-quality-posts/14147349) – sushildlh Nov 03 '16 at 08:36
  • Actually this link provide context to the answer @sushildlh –  Nov 03 '16 at 10:45
0

It's old, but hope this helps some one out in the future.

One use I found on real life scenario:

Assume you've a list of items that are stored into database, and you've 2 activities:

  1. DisplayActivity: which displayed these objects after getting them from database.
  2. EditActivity: used to edit an existing item & save that into database.

You decided to have a couple of options to go from DisplayActivity to EditActivity:

  • First: To add a brand-new item into database.
  • Second: To edit an existing item.

In order not to repeat yourself by duplicating code, you decided to use EditActivity for both purposes.

And so, you want to customize Options Menu according to each purpose. For this case you'd build a default options menu using onCreateOptionsMenu(), and leave it as-is when it's time to edit an existing item; and invalidateOptionsMenu() it when it's time to create new items; and in this case onPrepareOptionsMenu() is auto triggered for customizing your menu.

For instance the Options menu can have a delete option for editing an existing item, and this should be hidden when adding a new item.

Zain
  • 37,492
  • 7
  • 60
  • 84
0

From fragment call getActivity().invalidateOptionsMenu();.

CoolMind
  • 26,736
  • 15
  • 188
  • 224