I observed the same symptoms (as issue 133394 project) in a project with two activities A and B that extend ActionBarActivity . Activity A is the main action, and I always get null for savedInstanceState in onCreate its list fragment when I return from activity detail view B. After many hours, this problem turned out to be a masking problem for me.
The following actions may be relevant to my setup and are available from the other answers on this page:
- Given this , I made sure that each fragment and activity has unique identifiers.
onSaveInstanceState no onSaveInstanceState without calling super .- Action A is specified as the acitivy B parent in
AndroidManifest.xml , using both the android:parentActivityName attribute and the corresponding meta-data tag for earlier versions of Android (see "Providing Navigation" ).
Already without a corresponding creation code, such as getActionBar() .setHomeButtonEnabled(true) , activity B has a function return button ( < ) in the action bar. When this button is pressed, activity A appears, but with (a) all previous state of the instance is lost, (b) onCreate always called, and (c) savedInstanceState always null .
Interestingly, when I click the "Back" button located on the lower edge of the emulator screen (an open triangle pointing to the left), activity A reappears, as it remains (i.e., its state copy is fully saved) without calling onCreate . Maybe something is wrong with navigation?
After more reading, I followed my own navigation instructions to launch in response to pressing the return button in step B
@Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == android.R.id.home) NavUtils.navigateUpFromSameTask(this); return true; } return super.onOptionsItemSelected(item); }
Nothing is related to restoring the activity state of instance A. NavUtils also provides the getParentActivityIntent(Activity) and navigateUpTo(Activity, Intent) methods, which allow us to change the navigation intent to explicitly indicate that activity A is not started fresh (and thus without the instance state saved) by setting the FLAG_ACTIVITY_CLEAR_TOP flag:
If it is established and the launched activity is already running in the current task, then instead of launching a new instance of this activity, all other activities on top of it will be closed and this intention will be transferred (now from above) to the old activity as a new intention.
In my hands, this solves the problem of the lost state of the instance and might look like this:
public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId()== android.R.id.home) { Intent intent = NavUtils.getParentActivityIntent(this); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); NavUtils.navigateUpTo(this, intent); return true; } return super.onOptionsItemSelected(item); }
Please note that this may not be a complete solution in other cases, when the user can directly switch to activity B from another task (see here ). In addition, an identical solution to a behavior that does not use NavUtils is NavUtils is to simply call finish() :
public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId()== android.R.id.home) { finish(); return true; } return super.onOptionsItemSelected(item); }
Both solutions work in my hands. I just assume that the original problem is the incorrect standard implementation of the back button, and it could be related to this implementation, causing some kind of navigateUp that misses FLAG_ACTIVITY_CLEAR_TOP .