Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android savedInstanceState always null

Tags:

android

I have the following situation:

  • Activity A starts activity B
  • Activity A calls onPause, onSaveInstanceState and onStop methods.
  • On activity B I press the "UP button" on the action bar
  • Activity A first gets destroyed (calls 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 ..)
  • During the 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?

like image 430
Kuurde Avatar asked Mar 17 '14 11:03

Kuurde


People also ask

What is the use of savedInstanceState in Android?

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.

Is onSaveInstanceState always called?

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.

What is the use of protected void onCreate bundle savedInstanceState?

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.

What is onSaveInstanceState?

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.


2 Answers

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.

like image 93
ls.illarionov Avatar answered Oct 10 '22 18:10

ls.illarionov


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);
    }
}
like image 40
Greg Ennis Avatar answered Oct 10 '22 17:10

Greg Ennis