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
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 !=
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.
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.
Choose one: onSaveInstanceState() is called before the onStop() method. onSaveInstanceState() is called before the onResume() method.
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.
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().
You have to do super.onSaveInstanceState(outState) before adding content to it.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With