NullPointerException NullPointerException - 5 months ago 8
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

ONACTIVITYRESULT


wich method is called when I press back key?

I need to know it because I have to
override/implement
code on it

Answer

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

You can use this to target all Android versions:

@Override
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:

@Override
public void onBackPressed() {
    return;
}