Sending your application to background when backbutton is pressed in ANDROID…… OR Detecting whether back Button has been pressed in your ANDROID phone or not.

By | April 1, 2011

This code works for all API Levels.

public class MyActivity extends Activity {
  @Override
  public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
      moveTaskToBack(true);
    }
    return super.onKeyDown(keyCode, event);
  }
}

The below code works for API level 5 or Higher…..

public class MyActivity extends Activity {
   @Override
  public void onBackPressed() {
    moveTaskToBack(true);
  }
}

Leave a Reply

Your email address will not be published. Required fields are marked *