Need Help! Contact me.Email me Thank You.

How to override back button press in Android?

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() {
  4.     // implement your override logic here
  5.    return;
  6. }
Subscribe to DeveloperFeedDid you like this article? Did it Help you Solve your Problem? You can get the all the latest articles published at DeveloperFeed in your email inbox by entering your email address below. Your address will only be used for mailing you the articles, and each one will include a link so you can unsubscribe at any time.