android savedInstanceState always null

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);
    }
}

Tags:

Android