Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Refresh Options Menu without calling invalidateOptionsMenu()

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()?

like image 461
Michael 'Maik' Ardan Avatar asked Feb 14 '13 03:02

Michael 'Maik' Ardan


People also ask

What is invalidateOptionsMenu in Android?

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.

How to inflate a menu in Android?

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.

What is onCreateOptionsMenu in Android?

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.

Which method is used to handle the events in option menu?

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.


1 Answers

@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()

like image 89
Michael 'Maik' Ardan Avatar answered Sep 19 '22 16:09

Michael 'Maik' Ardan