Which is the function called when you click the back button on android. My requirement is when an application is running and user clicks on the back button, the status of the application should be stored into the database and the user should be able to see the status whenever he returns back to the application.
Browsing through internet, I understood we can handle the control using onKeyDown() function. However even if I use it in my code, the function is not called when I click on back button. Below is the function:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
Log.d(null,"In on Key Down");
if (keyCode == KeyEvent.KEYCODE_BACK) {
moveTaskToBack(true);
return true;
}
return super.onKeyDown(keyCode, event);
}
Please suggest, if anybody has faced the same/similar scenario.
Use the Activity lifecycle methods to save and restore state. You could save your state during onPause and restore in onResume. In your activity:
You could also do this in onStart/onStop. Check out the activity lifecycle here: http://developer.android.com/reference/android/app/Activity.html