Dismiss Popup When Options Menu Is Pressed
I implemented a popup which shows up when I press the hardware menu key. Now I want to dismiss the popup when I press on the menu key again. I tried this @Override public boolean o
Solution 1:
Apparently, pressing Menu Key second time does not trigger. Found here the solution. Hope it helps.
EDIT : Checked and created another approach. This should do it :
booleanopen=false;
booleanitemSelected=false;
@OverridepublicbooleanonMenuOpened(int featureId, Menu menu) {
if(!open || itemSelected) {
showPopup();
open = true;
itemSelected = false;
}
returnfalse;
}
@OverridepublicvoidonPanelClosed(int featureId, Menu menu) {
if (open && !itemSelected) {
closePopup();
open = false;
}
}
@OverridepublicbooleanonMenuItemSelected(int featureId, MenuItem item) {
itemSelected = true;
returnfalse;
}
Works for me on Android 4.0.3. Apparently onPrepareOptionsMenu
has changed functionality after v3.
Solution 2:
Why don't you do this ?
privatebooleanpopUpShowed=false;
@OverridepublicbooleanonKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
if (popUpShowed) {
pw.dismiss();
popUpShowed = false;
} elseif(!popUpShowed){
openpopup();
popUpShowed = true;
}
returntrue;
}
returnsuper.onKeyDown(keyCode, event);
}
And delete the keyListener on your popup.
Solution 3:
try this:
@OverridepublicbooleanonKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
if (pw.isShowing()) {
closePW();
} else {
openpopup(); // <- I think you do this already here
}
returntrue;
}
returnsuper.onKeyDown(keyCode, event);
}
}
// Put this function in the class which open the popup// like your function openpopup()publicvoidclosePW(){
pw.dismiss();
}
Post a Comment for "Dismiss Popup When Options Menu Is Pressed"