I have the following situation:
onPause
, onSaveInstanceState
and onStop
methods.onDestroy
) and then recreated. (This just seems to happen this way. I have not implemented this way, just seems to be the Android way of doing things ..)onCreate
method, the variable savedInstanceState
is always null.I know there have been similar topics around here, but none of them seem to have an answer for my situation. All the callback methods have log lines in them, so I'm certain that the save method is executed and destory is executed. But why is there never a savedInstanceState
object?
My save method:
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putSerializable(OpenSessionsActivity.SESSION, session);
System.out.println("saving ..");
super.onSaveInstanceState(savedInstanceState);
}
Is there any other code you need me to include?
The savedInstanceState is a reference to a Bundle object that is passed into the onCreate method of every Android Activity. Activities have the ability, under special circumstances, to restore themselves to a previous state using the data stored in this bundle.
I'm aware that onSaveInstanceState() is not always called when an activity is about to be destroyed. For example, if it is destroyed because the user has pressed the "back" button, the activity state is not preserved.
After Orientation changed then onCreate(Bundle savedInstanceState) will call and recreate the activity and load all data from savedInstanceState. Basically Bundle class is used to stored the data of activity whenever above condition occur in app.
Configuration change scenario Note that onSaveInstanceState() is called when your activity goes into the background and NOT when the app process is about to be killed.
The top activity is recreated when navigating Up. To preserve state of the activity A you can
A) Set launch mode of the activity A to "singleTop" (add android:launchMode="singleTop"
to AndroidManifed.xml)
or
B) Add FLAG_ACTIVITY_CLEAR_TOP
flag to the up
intent when navigating from the activity B:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent up = NavUtils.getParentActivityIntent(this);
up.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
NavUtils.navigateUpTo(this, up);
return true;
}
return super.onOptionsItemSelected(item);
}
This is a documented behavior:
Similarly, if you navigate up to an activity on the current stack, the behavior is determined by the parent activity's launch mode. If the parent activity has launch mode singleTop (or the up intent contains FLAG_ACTIVITY_CLEAR_TOP), the parent is brought to the top of the stack, and its state is preserved. The navigation intent is received by the parent activity's onNewIntent() method. If the parent activity has launch mode standard (and the up intent does not contain FLAG_ACTIVITY_CLEAR_TOP), the current activity and its parent are both popped off the stack, and a new instance of the parent activity is created to receive the navigation intent.
Press Up
on the action bar is actually not the same thing as pressing the back
button.
If you want them to behave the same, you could use this code in Activity B:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With