How to override back button press in Android?

November 01, 2010

In order to capture or override the default back button press in Android the following onKeyDown method can be implemented by the Activity.

  1. @Override
  2. public boolean onKeyDown(int keyCode, KeyEvent event) {
  3.     if (keyCode == KeyEvent.KEYCODE_BACK) {
  4.         moveTaskToBack(true);
  5.         return true;
  6.     }
  7.     return super.onKeyDown(keyCode, event);
  8. }

In case of Anroid 2.0+ a convience method is provided as

  1. @Override
  2. public void onBackPressed() {
  3.  
  4.     // implement your override logic here
  5.    return;
  6. }
Leave a reply
Software for WebmastersSome Interesting Facts about Internet
Sponsors
Like us on Facebook