Reputation: 3127
I have two menu items, with id R.id.start
and R.id.stop
. At the beginning only "start: is visible. Now I want to change visibility of both of them when any of them are selected.
I tried to do this by calling invalidateOptionsMenu()
and then on onPrepareOptionsMenu(menu)
I changed visibility. The problem is that I'm getting empty menu
. This is probably because menu
wants to recreate and I have empty onCreateOptionsMenu(menu)
and I'm not creating one, after it was created before.
Of course I could recreate it, but it is not needed. Is there any way to tell android to call onPrepareOptionsMenu
but without calling onCreateOptionsMenu
?
Upvotes: 0
Views: 183
Reputation: 884
You dont actually need onPrepareOptionsMenu I used to use that and the results weren't as pretty.
In your class make an integer(e.g. mine is called activated) and change the integer values whenever you want like so and then invalidate your options menu :
onClick(View v)
{
activated=1;
getActivity().invalidateOptionsMenu();
}
Now in your options menu, check for the value of the integer, clear the menu then inflate accordingly(you can inflate as many as you want) :
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
if(activated==1)
{
menu.clear(); //this makes all the menus invisible
//my menu is called secret
getActivity().getMenuInflater().inflate(R.menu.secret, menu);
//you mgiht want to inflate another one here
activated=0;
}
else if(activated==2)
{
//etc
}
else
{
//etc
}
}
Upvotes: 1