NullPointerException NullPointerException - 10 months ago 27
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


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() {