Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of invalidateOptionsMenu() in android

I am a newbie to android when I am going through a sample code for navigation drawer I found he called the method invalidateOptionsMenu() so I searched regarding its functionality but couldn't find an answer so can anyone please brief me its functionality and whenshould we use that.

like image 381
srujan maddula Avatar asked Mar 13 '14 09:03

srujan maddula


2 Answers

This function tell android that it should redraw the menu. By default, once the menu is created, it won't be redrawn every frame (since that would be useless to redraw the same menu over and over again).

You should call this function when you changed something in the option menu (added an element, deleted an element or changed a text). This way android will know that it's time te redraw the menu and your change will appear.

Hope this answers your question

like image 53
Basile Perrenoud Avatar answered Sep 27 '22 19:09

Basile Perrenoud


I use this method in combination with actionbar: When I need to populate actionbar with new menu items, I call invalidateOptionsMenu(), then onCreateOptionsMenu is called and I can inflate menu that I need. :-)

for more info see http://developer.android.com/reference/android/app/Activity.html#invalidateOptionsMenu() or Change options menu during runtime - invalidateOptionsMenu()

like image 42
Berťák Avatar answered Sep 27 '22 18:09

Berťák