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.
Put the initial state of the menu in onCreateOptionsMenu(...) Use the invalidateOptionsMenu() to force onCreateOptionsMenu(...) and onPrepareOptionsMenu(...) In onPrepareOptionsMenu(...), call menu. clear() to remove all items from the menu.
Use onPrepareOptionsMenu if you need to enable/disable, show/hide, or add/remove items after creating it.
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().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With