In my activity's onCreateOptionsMenu
method, I am simply inflating my menu layout file and attaching it.
I want to show/hide some menu items based on a value a of a global variable, I do this in the onPrepareOptionsMenu
method. I've read that this is the correct place to do it.
My onPrepareOptionsMenu
method doesn't always fire. I don't know why but it doesn't always fire when I press the "Menu" button on my phone. Maybe it has some thing to do with it's internal state.
It seems to fire when the Acitvity is being created. Pressing the "Menu" button for the first time, doesn't cause it to fire but if I press the menu button a second time, it works just fine.
Is there a way I could force the onPrepareOptionsMenu
to fire.
Thanks
Smok suggested using the invalidateOptionsMenu
method to invalidate the menu items but this causes the onCreateOptionsMenu
method to fire too. Here's my method:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
System.out.println("onCreate");
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.search, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconified(false);
searchView.requestFocusFromTouch();
return true;
}
@Override
public boolean onPrepareOptionsMenu (Menu menu) {
System.out.println("prepared");
if (this.objAdapter == null) {
menu.findItem(R.id.sort).setVisible(false);
menu.findItem(R.id.filter).setVisible(false);
menu.findItem(R.id.group).setVisible(false);
} else {
menu.findItem(R.id.sort).setVisible(true);
menu.findItem(R.id.filter).setVisible(true);
menu.findItem(R.id.group).setVisible(true);
}
return true;
}
As you can see from my onCreateOptionsMenu
method, invoking it again will cause the focus to be lost to the SearchView
.
As Smok pointed out: by calling invalidateOptionsMenu()
.
I had the same issue and I fixed with supportInvalidateOptionsMenu() method (because I use support activity) but with a workaround.
I have a flag variable that I check in the onCreateOptionsMenu(Menu menu) method in this way:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!IS_MENU_FIXED) {
// Create menu only the first time the activity is loaded
super.onCreateOptionsMenu(menu);
getSupportMenuInflater().inflate(R.menu.main, menu);
// Create menu items, etc...
}
return true;
}
And this is the way I call supportInvalidateOptionsMenu() when is required:
if (!IS_MENU_FIXED) {
supportInvalidateOptionsMenu();
IS_MENU_FIXED = Boolean.TRUE;
}
This is the Official documentation about this topic.
Regards, Gabriel.
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