i'm using this code to open a optionsmenu as a popup:
@Override
public boolean onPrepareOptionsMenu(Menu menu){
openMenu();
return true;
}
private void openMenu(){
View popUpView = getLayoutInflater().inflate(R.layout.menu, null);
popUpMenu = new PopupWindow(popUpView, LayoutParams.FILL_PARENT
, LayoutParams.WRAP_CONTENT
, true);
popUpMenu.setBackgroundDrawable(new BitmapDrawable());
popUpMenu.setAnimationStyle(android.R.style.Animation_Dialog);
popUpMenu.showAtLocation(popUpView, Gravity.BOTTOM, 0, 0);
makePopUpMenuButtons(popUpView);
}
The Problem is, that on Android 4.1 the menu only open once. I read something about invalidateOptionsMenu(), but i don't know where to implement this method. Also eclipse says "The method invalidateOptionsMenu() isn't defined on...".
Does anyone know what to do so that the optionsmenu will open every time when the menu button is clicked?
Edit:
I don't use the android menu object, because i want to have a custom design. That's the reason why i create a popop in onPrepareOptionsMenu. So i don't use onOptionsItemSelected. My problem is that onPrepareOptionMenu is only called on the first click and after that only irregular.
On Android 3.0 and higher, the options menu is considered to always be open when menu items are presented in the action bar. When an event occurs and you want to perform a menu update, you must call invalidateOptionsMenu() to request that the system call onPrepareOptionsMenu().
This paragraph says i have to call invalidateOptionsMenu when my API is 3.0 and higher. But in my project all works fine with APIs lower then 4.1. I tried to call invalidateOptionsMenu after showing my popup, but eclipse gives an error, because the method is undefined...
This code solved my problem:
But i don't know if it's the best possible way