I am a noob to android and i am trying to inflate two different menus depending on user selection. However, the menu isn’t switching. The same menu inflates everytime regardless of what the user selects. I’ve tried and checked my if/else statmente with various parameters and the menu inflater still doesn’t respond properly by only inflating the same menu. Any help is greatly appreciated.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu);
popUpMenu = getMenuInflater();
//popUpMenu.inflate(R.menu.cool_menu, menu);
if(mypodcast==null){
popUpMenu.inflate(R.menu.cool_menu, menu);
}else {
popUpMenu.inflate(R.menu.podcast, menu);
}
return true;
}
That is because
onCreateOptionsMenu()is not called each time you open the menu, from the documentation:If you want to change menus you need to do this in
onPrepareOptionsMenu(). However I don’t believe that you can or should inflate a new menu every timeonPrepareOptionsMenu()is called. But you can combine the two menus and change the visibility of each menu item according to what you want in this method.