SavedInstanceState is always null

This is my saveInstaceState code:

@Override public void onSaveInstanceState(Bundle savedInstanceState) { savedInstanceState.putStringArrayList("todo_arraylist", Altodo); Log.v("bundle", "Saved"); super.onSaveInstanceState(savedInstanceState); } public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (savedInstanceState != null) { Altodo = savedInstanceState.getStringArrayList("todo_arraylist"); Log.v("bundle", "Restored"); } else { Log.v("bundle", "null"); } setContentView(R.layout.main); } 

Logs always display the "Save Package" tag.

But in onCreate method SavedInstanceState always null.

+67
android
Jul 02 2018-11-11T00:
source share
13 answers

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 .

+50
Apr 6 '15 at 1:14
source share

You checked if you have an identifier for this view (if it has / has ...). onSaveInstanceState () is not called otherwise.

Check out the link.

+14
Aug 12 '11 at 11:33
source share

The state saved in this way is not saved. If the entire application is killed, as you do during debugging, the package will always be null in onCreate .

This IMO is another example of terrible documentation for Android. This also explains why most applications on the market do not implement the save state properly (in general).

+13
Feb 11 '12 at 17:59
source share

in the manifest add this line for action

 android:launchMode="singleTop" 

eg:

 <activity android:name=".ActivityUniversity" android:label="@string/university" android:launchMode="singleTop" android:parentActivityName="com.alkhorazmiy.dtm.ActivityChart"> <meta-data android:name="android.support.PARENT_ACTIVITY" android:value="com.alkhorazmiy.dtm.ActivityChart" /> </activity> 
+7
May 15, '15 at 14:07
source share

How do you test it?

Imo the best way to check if "Do not save actions" uses the -flag in "Settings"> "Developer options". If you do not have developer options in the settings, see Enabling developer options on the device .

  • Discover your activity.
  • The long-awaited home
  • Go to another application
  • The long-awaited home
  • Get back to your app.
+3
Jul 02 2018-11-11T00:
source share

Shouldn't super.onSaveInstanceState(savedInstanceState); be the first line in your redefinition?

Edit : War_Hero notes in the comments that the documentation on this issue indicates no, t is the first line.

+3
Jul 02 2018-11-11T00:
source share

Test your activity in AndroidManifest.xml and remove the android:noHistory , if true.

 <activity // .... android:noHistory="false" /> 
+1
Aug 21 '16 at 6:24
source share

To debug, consider deploying onRestoreInstanceState and place the call in Log.d in this method. Then, in the emulator, press ctrl-F11 or anything to rotate the phone. Your call to Log.d should be deleted.

0
Jul 02 2018-11-11T00:
source share

Implement the onRestoreInstanceState method and put the code below there

 Altodo = savedInstanceState.getStringArrayList("todo_arraylist"); 
0
Jul 02 '11 at 12:15
source share

I found that when I override onSaveInstanceState() and actually save some data in the Bundle , the state of the instance is restored. Otherwise, it is not.

0
Jan 27 '17 at 9:59 on
source share

Ive succeeded just like arround. Instead of processing the saveInstanceState Bundle in the onCreateView method, I processed it in the onCreate method and set the passed value to the globar variable, and then processed this variable in the onCreateView method. Hope it helps.

0
Sep 04 '18 at 1:15
source share

https://developer.android.com/guide/topics/manifest/activity-element#lmode

This shows that "similarly, if you go to an action in the current stack, the behavior is determined by the trigger mode of the parent action." You may be in "standard" mode.

0
Jul 24 '19 at 5:00
source share

I was able to solve this with:

 @Override public boolean onSupportNavigateUp() { onBackPressed(); return true; } 

the parent element was still set in the manifest. Therefore, when you press the navigation button up, it now acts as a return button.

0
Aug 08 '19 at 3:46
source share



All Articles