Breaking News
Home / Android / Android Snippets / How to override back button press in Android?

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() {
  3.  
  4.     // implement your override logic here
  5.    return;
  6. }
[pb_builder]

Check Also

How to check if an Android device can connect to Internet?

The following code snippet shows how to check if an Android device can connect to …

Advertisment ad adsense adlogger