According to http://developer.android.com/training/basics/activity-lifecycle/recreating.html
There are various ways to trigger activity recreation.
- Screen rotation
- Low memory condition
I realize screen rotation and low memory condition yield quite different behavior.
One of the obvious observations is that, for restore activity from long pressed home, it will destroy and re-create Application
as well.
For screen rotation, it will not yield such behavior.
May I know, how can Activity
or Fragment
differentiate both cases?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Check whether we're recreating a previously destroyed instance
if (savedInstanceState != null) {
// Is this caused by screen rotation? Or restoration from low memory condition?
// How can we differentiate among "screen rotation", or "restoration from low memory condition"?
} else {
}
...
}
p/s To produce low memory condition, here are the steps to be done.
- Press home to put the app in back stack.
- Launch a memory intensive app.
- Press home.
- Repeat steps 2-3 for 5 times for other apps.
- Launch the 1st app again.
- You will realize
savedInstanceState
is not null. However, at the same time, you will realize that current runningApplication
instance is different than the first time launched's.
Besides static members will become uninitialized when restoring from low memory condition, I also encounter some weird stuffs
- Launch an child activity from parent activity via
startActivityForResult
- Perform the above 6 steps.
- Close the child activity.
- We can observe parent activity's fragment is having the following life cycle.
onCreate
->onActivityResult
->onResume
We are expecting onCreate
-> onResume
-> onActivityResult