16

Saving bundle (activity A):

@Override
protected void onSaveInstanceState(Bundle outState) {
    outState.putString("test", "value");
    super.onSaveInstanceState(outState);
}

Navigating to activity B;

startActivity(new Intent(getBaseContext(), B.class));

Going back to activity A:

startActivity(new Intent(getBaseContext(), A.class));

Trying to load the value I wrote to bundle in activity A:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

Log.d("MY", "saved instance is null"+ Boolean.toString(savedInstanceState == null));
}

Returns always savedInstanceState = null. What I'm missing here?

onRestoreInstanceState is never triggered when I return to main activity

Indrek Kõue
  • 6,449
  • 8
  • 37
  • 69

6 Answers6

6

I got this solve by having:

intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

when re-launching the old intent that has the saved state from background. Without this flag, I think that when startActivity is called, it creates a new instance of the Activity instead of getting the old one from the stack.

Martin Geisler
  • 72,968
  • 25
  • 171
  • 229
leo
  • 61
  • 1
  • 3
5

There is nothing wrong with your code. Try rotating the device when on activity A. You will see the following in the Log, which means your onSaveInstanceState() is working fine:

saved instance is nullfalse

Here are an excerpt from the Android Developer Site, which you may find interesting:

The callback method in which you can save information about the current state of your activity is onSaveInstanceState(). The system calls this method before making the activity vulnerable to being destroyed and passes it a Bundle object. The Bundle is where you can store state information about the activity as name-value pairs, using methods such as putString(). Then, if the system kills your activity's process and the user navigates back to your activity, the system passes the Bundle to onCreate() so you can restore the activity state you saved during onSaveInstanceState(). If there is no state information to restore, then the Bundle passed to onCreate() is null.

Note: There's no guarantee that onSaveInstanceState() will be called before your activity is destroyed, because there are cases in which it won't be necessary to save the state (such as when the user leaves your activity using the BACK key, because the user is explicitly closing the activity). If the method is called, it is always called before onStop() and possibly before onPause().

OceanBlue
  • 9,142
  • 21
  • 62
  • 84
  • `... Then, if the system kills your activity's process and the user navigates back to your activity, the system passes the Bundle to onCreate() ...` - isn't my activity killed when user navigates to another activity? – Indrek Kõue Sep 06 '11 at 15:07
  • Try logging onDestroy() :-). No, in this case, it isn't. A new instance of the Activity is being created every time the line startActivity(...A.class) is running. – OceanBlue Sep 06 '11 at 15:12
  • Yes, you'r are right. Reference: http://developer.android.com/images/activity_lifecycle.png To sum up: **Activity gets stopped when user navigates to another activity. Activity doesn't get destroyed until system says so (for example not enough memory).** – Indrek Kõue Sep 07 '11 at 07:22
  • You can change the OS behavior so the activity is destroyed as soon as it's no longer on the screen. This is useful for debugging purposes. See this answer: http://stackoverflow.com/a/8621269/483708 – Theo Dec 24 '11 at 00:00
2

You have to do super.onSaveInstanceState(outState) before adding content to it.

NujnaH
  • 173
  • 1
  • 8
  • Nope, not working. And I'm quit sure that the `super.onSaveInstanceState(outState);` has to be at the end. Reference: http://stackoverflow.com/questions/151777/how-do-i-save-an-android-applications-state – Indrek Kõue Sep 06 '11 at 13:48
  • It hast to be at the start. Maybe this was not the OP's issue, but just clarifying that it indeed is the first thing you should call. – blindstuff Dec 06 '11 at 19:35
  • 3
    Actually, it doesn't matter as long as you don't add anything to the outState with an ID that overlaps anything from the OS. http://stackoverflow.com/questions/10342274/call-super-onsaveinstancestatebundle-first-or-last – Dave Oct 04 '13 at 13:04
  • @blindstuff Not according to Android's [documentation and code examples.](http://developer.android.com/training/basics/activity-lifecycle/recreating.html#SaveState) – Lo-Tan Feb 10 '15 at 17:25
2

If navigating to another Activity and coming back, best way is to save it to SharedPreference in the onPause() method, which is bound to execute when a new activity is loaded. On the other Activity, the value can be accessed in onCreate() by accessing the shared preference.

cha0site
  • 10,517
  • 3
  • 33
  • 51
0

In case you want to retain the state of your fragment over a rotation of the device and you wonder that onSaveInstanceState is never called:

  1. Check whether you call super.onSaveInstanceState(outState); in all your onSaveInstanceState functions.
  2. Check that you don't have android:configChanges in your AndroidManifest.xml.
codingdave
  • 1,207
  • 16
  • 23
0

Unless your app is running on Lollipop (API21) version of Android or newer, your

public void onSaveInstanceState (Bundle outState, PersistableBundle outPersistentState);

will NOT be called as it simply does not exist on earlier versions of the platform than 21. To support pre API 21 devices you must, instead of the above, override the following method:

public void onSaveInstanceState (Bundle outState);

This will work on API 21+ as well, so you do not need to override both methods, of course (unless you know you need to deal with PersistableBundle the new one offers).

(Copied because it worked for me) Source: onSaveInstanceState is not getting called after screen rotation