Hello Android Developers,
I have seen a lot of question regarding the update of Options Menu. However, every answer says I have to call invalidateOptionsMenu()
.
My question is, is there any other way of updating the Options Menu without invoking the method invalidateOptionsMenu()
?
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.
Implement the onCreateContextMenu() method in your Activity or Fragment . MenuInflater allows you to inflate the context menu from a menu resource. The callback method parameters include the View that the user selected and a ContextMenu.
If you've developed for Android 3.0 and higher, the system calls onCreateOptionsMenu() when starting the activity, in order to show items to the app bar.
You will use onTouch() event handler to handle such event. This is called when the user selects a menu item. You will use onMenuItemClick() event handler to handle such event.
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.clear(); // Clear the menu first
/* Add the menu items */
return super.onPrepareOptionsMenu(menu);
}
This solved the problem on updating the OptionsMenu without invoking the invalidateOptionsMenu()
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