NullPointerException NullPointerException -5 years ago 105
Android Question

Which method is called when i press back key on my phone?

I have an app that has a lot of activities, all created with


wich method is called when I press back key?

I need to know it because I have to
code on it

Answer Source

The method that is called for is: onKeyDown of your current Activity.

You can use this to target all Android versions:

public boolean onKeyDown(int keyCode, KeyEvent event)  {
      if (keyCode == KeyEvent.KEYCODE_BACK) {
          return true;

   return super.onKeyDown(keyCode, event);

If you're targeting newer Android devices, the API for Android 2.0 and above has made things a little easier:

public void onBackPressed() {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download