When you pressed "home" button, your activity goes to onPause()
.
So I personally recommend you to override the onPause()
method which can not only handle the "home" button pressed but also other circumstances.
In your case, it is only onPause()
and onResume()
related, so you can try put the state into SharedPreferences
or Internal/External storage.
say:
in your onPause()
method, do something like:
// Use Shared Preferences to save data
SharedPreferences previewSizePref = getSharedPreferences("PREF",MODE_PRIVATE);
SharedPreferences.Editor prefEditor = previewSizePref.edit();
prefEditor.putInt("x", somethingA);
prefEditor.putInt("y", somethingB);
prefEditor.commit();
and in your onResume()
, retrieve the saved data like:
SharedPreferences previewSizePref = getSharedPreferences("PREF",MODE_PRIVATE);
if (previewSizePref.contains("x") && previewSizePref.contains("y")) {
//your saved data exists, do something
} else {
// handle the circumstances that the saved data doesn't exist
}
For saving state in onPause()
and restore in onResume()
you can have a look at this answer:
Saving Activity State in the onPause
And for SharedPreferences, you can have a look at API document: Data Storage - Shared Preferences