Search code examples
androidnavigationcontextmenuback

Android context button blocks back button functionality


I have a problem in all my activities, when I press the back button it works properly, but when I press the context menu button before, nothing happens when I press back. I am talking about the phone buttons, not the toolbar icon for back navigation. Has anyone ever dealt with this?

EDIT: No menu shows, onPrepareOptionsMenu() is removed, the phone vibrates when I press back but no action. I see that if I inflate a menu, it worsk properly after the first back that closes te popup.

I see that this happens only if I set the toolbar with setSupportActionBar(toolbar);


Solution

  • I have nearly the same problem, this is my case:

    • AppCompatActivity with toolbar set with setSupportActionBar(toolbar);
    • I'm using a navigation Drawer (dont know if this can affect)
    • onBackPressed() is overriden to prompt logout dialog

    All works fine until i press the device menu button, then the app begins to ignore the back button.

    This is what i have seen after pressing the device menu button

    • The method onBackPressed() is not called after this
    • If you set an onKeyUp(int keyCode, KeyEvent event), it receives the event (and you can check that de keyCode is the same as KeyEvent.KEYCODE_BACK)

    I don't know why the onBackPressed stops from being called, tried without overriding onBackPressed and onKeyUp and the problem is still the same.

    I know this is not the best solution, but to fix this problem i'm using the next code:

    @Override
        public boolean onKeyUp(int keyCode, KeyEvent event) {
            if (keyCode==KeyEvent.KEYCODE_BACK){
                    onBackPressed();
                    return true;
            }
            return super.onKeyUp(keyCode, event);
        }
    

    Hope this helps.