Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onSaveInstanceState is not saving my values ( onCreate input Bundle is always null )

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

like image 279
Indrek Kõue Avatar asked Sep 06 '11 13:09

Indrek Kõue


People also ask

Why is savedInstanceState null?

savedInstanceState will always remain null after the app is killed. It is not passed on to the bundle. public void onCreate(Bundle savedInstanceState) { super. onCreate(savedInstanceState); if (savedInstanceState !=

What is the purpose of the bundle argument to activity onCreate?

Basically Bundle class is used to stored the data of activity whenever above condition occur in app. onCreate() is not required for apps. But the reason it is used in app is because that method is the best place to put initialization code.

What is the purpose of using onSaveInstanceState () here?

Basically, onSaveInstanceState is used for the scenario where Android kills off your activity to reclaim memory. In that scenario, the OS will keep a record of your activity's presence, in case the user returns to it, and it will then pass the Bundle from onSaveInstanceState to your onCreate method.

When onSaveInstanceState () is called?

Choose one: onSaveInstanceState() is called before the onStop() method. onSaveInstanceState() is called before the onResume() method.


4 Answers

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.

like image 117
leo Avatar answered Oct 04 '22 13:10

leo


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().

like image 20
OceanBlue Avatar answered Oct 04 '22 13:10

OceanBlue


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

like image 22
NujnaH Avatar answered Oct 04 '22 15:10

NujnaH


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.

like image 38
user1169529 Avatar answered Oct 04 '22 14:10

user1169529