There are two ways for your purpose:
first: Override the onBackPressed method in Activty :
@Override public void onBackPressed() { super.onBackPressed(); }
2nd: override onKeyDown and find KeyEvent.KEYCODE_BACK
public boolean onKeyDown(int keyCode, KeyEvent event){ if (keyCode == KeyEvent.KEYCODE_BACK) { } return false; }
If you want to combine both (let's say you want BackAction on the menuButton button, it will look like this:
public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_MENU) { onBackPressed(); } return false; }
Thkru source share