Kaloyan Roussev
Kaloyan Roussev

Reputation: 14711

After clicking hardware Menu button, onBackPressed is not called when hardware Back button is clicked

I have a very strange problem with my LG G Pro It has a hardware Back, Home and Menu buttons. In an activity in the application, if I click the hardware Menu button, "onBackPressed" doesn't get called anymore if I tap the hardware Back button.

I tried overcoming this using this piece of code:

@Override
public boolean onKeyUp(int keycode, KeyEvent e) {
    switch(keycode) {
        case KeyEvent.KEYCODE_MENU:
            if (menu != null) {
                Log.e("Activity", "onKeyUp KEYCODE_MENU");
                return true;
            }
    }
    return super.onKeyUp(keycode, e);
}

But this doesnt help.

Here is some more code from the activity:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    this.menu = menu;
    String buttonText = getString(R.string.action_bar_done);
    MenuItem item = menu.add(MENU_GROUP, MENU_ITEM_DONE_NUMBER, 0, buttonText);
    item.setTitle(buttonText);
    item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
    return super.onCreateOptionsMenu(menu);
}

Upvotes: 1

Views: 179

Answers (1)

spawnpeekboi
spawnpeekboi

Reputation: 69

I think you want to do this

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

Upvotes: 1

Related Questions