Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a conditional invalidateOptionsMenu() call depending on the API level in Android?

Tags:

android

I'm trying to update the menu buttons of my app every time one of them is pressed; in API 11+, you need to call invalidateOptionsMenu() to do this; since I want my app to be compatible with lower APIs, how do I only call this method when using api 11+?

like image 275
yuttadhammo Avatar asked Oct 24 '11 16:10

yuttadhammo


3 Answers

Use

ActivityCompat.invalidateOptionsMenu(Activity activity)

from the compatibility library.

like image 136
candrews Avatar answered Oct 30 '22 00:10

candrews


For the others who are looking for an answer like I was:

If you are using ActionBarSherlock and trying to refresh the action bar buttons on API <11, instead of

Activity.invalidateOptionsMenu()

you can use

SherlockActivity.supportInvalidateOptionsMenu():

like image 44
Czechnology Avatar answered Oct 29 '22 22:10

Czechnology


If you are extending ActionBarActivity in your class then you just need this:

supportInvalidateOptionsMenu();
like image 30
RicNjesh Avatar answered Oct 30 '22 00:10

RicNjesh