1

I am wanting a certain flag in my code to be set when a menu is on screen. I tried catching the keyCode == KeyEvent.KEYCODE_MENU event in onKeyDown to detect when the menu would appear - but then if the menu button is pressed a second time (making the menu disappear), there is no keyCode == KeyEvent.KEYCODE_MENU event.

Should I look for some other event, or is there a function to test if some kind of menu is on display?

Mick
  • 8,284
  • 22
  • 81
  • 173
  • Check my answer how easy to detect home,recent and power button. Check [this](http://stackoverflow.com/a/29648460/1071545) – Ahmad Arslan Apr 15 '15 at 11:06

1 Answers1

2

To catch the menu being opened, you can override

public boolean onPrepareOptionsMenu(Menu menu)

To catch the menu being closed, you can override

public void onOptionsMenuClosed(Menu menu)

However keep in mind that the second one will be called both when the user presses "menu" button the second time or taps one of the menu options. Therefore you'll need to use it in combination with

public boolean onOptionsItemSelected(MenuItem item)
Aleks G
  • 56,435
  • 29
  • 168
  • 265
  • Not working in my case.please refer this http://stackoverflow.com/questions/15652444/not-able-to-dismiss-the-alertdialog-when-pressing-menu-key-second-time – Abhijit Chakra Mar 27 '13 at 06:22
  • I am lilterally very disapointed why ppl gave marked answer to the actual wrong answer. – Ahmad Arslan Apr 15 '15 at 11:08
  • 1
    @ArslanAhmad Care to explain why believe this answer is wrong? It worked perfectly well two years ago when the question was asked - and it still works well. – Aleks G Apr 15 '15 at 19:58
  • @AleksG I have tried this 1000s time even my friends and coleagues but this method doesnot detect the Home , Recent Apps button. If you think it works give me the sample code If it runs i will give you 5 + votes from my friends – Ahmad Arslan Apr 16 '15 at 05:59
  • @ArslanAhmad Of course it does not. The question was specifically about the menu button - and so was the answer. – Aleks G Apr 16 '15 at 06:53