0

I notice that when implementing onPrepareOptionsMenu() in my activity - the first press works good - the callback from onPrepareOptionsMenu() starts. but when it still visible(open some dialog) and I want the second press to close it(trigger the callback to close the dialog) - the second press on the menu button doesn't triggers onPrepareOptionsMenu(). don't sure why

This is how I implement it:

 @Override
public boolean onPrepareOptionsMenu(Menu menu) {

    if (onDoneListener != null) {
        onDoneListener.OnDone();

    }
    return false;
}

EDIT1:

I added the next function after the commenter help but still no luck. The OnKeyDown() also not receiving the next menu button press. It looks like the menu button doesn't get events until I'm pressing the back button. Here is the code:

    @Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_MENU) {
        if (onDoneListener != null)
            onDoneListener.onDone();

    }
    return super.onKeyDown(keyCode, event);
}

I also tried to return true and false but not luck.

Nativ
  • 3,092
  • 6
  • 38
  • 69

1 Answers1

0

What you are seeing is the exact intended behavior of OnPrepareOptionsMenu. It runs before the menu is shown, not after.

salezica
  • 74,081
  • 25
  • 105
  • 166
  • 1
    yes but I return false, and according to documentation "You must return true for the menu to be displayed; if you return false it will not be shown." So the menu doesnt really visible – Nativ Mar 12 '13 at 11:39
  • eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeh? – salezica Mar 12 '13 at 23:36
  • 1
    If the menu doesn't loaded so it make sense this function be called again. and anyway, do you know how can I call my callback each time the menu button getting pressed? thanks – Nativ Mar 13 '13 at 08:58
  • 1
    Yes: you need to intercept the key itself, not the menu event. [Here you have an example](http://stackoverflow.com/questions/2478418/android-how-can-i-set-a-listener-to-the-menubutton) – salezica Mar 13 '13 at 19:44