Reputation: 777
so i have this code in my OnsavedInstanceState
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
String [] a={"haha"};
savedInstanceState.putStringArray("MyStringarray", a);
Toast.makeText(context, "Saved array", Toast.LENGTH_SHORT).show();
}
and i have this code in my onCreate
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(savedInstanceState==null){
Toast.makeText(this, "not there", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, "is there", Toast.LENGTH_SHORT).show();
}
}
how come the toast always says not there? i opened the app then switched to another app and it showed the toast saved array but when i reopen the app it says not there even though the bundle should have the string array containing "haha".
Many thanks!
Upvotes: 1
Views: 9814
Reputation: 1767
The problem might be in how you have your activities defined in your manifest. For instance if your activity has the setting android:clearTaskOnLaunch="true" I don't think you will receive the saved bundle. See http://developer.android.com/guide/topics/manifest/activity-element.html for details on the various activity settings.
You might also check the other overridden methods. For example in you override one and do something odd you could mess the activity stack up. Do you call finish() anywhere in you code, if so remove it and see what happens.
Upvotes: 1
Reputation: 5391
Do not confuse this method with activity lifecycle callbacks such as onPause(), which is always called when an activity is being placed in the background or on its way to destruction, or onStop() which is called before destruction. One example of when onPause() and onStop() is called and not this method is when a user navigates back from activity B to activity A: there is no need to call onSaveInstanceState(Bundle) on B because that particular instance will never be restored, so the system avoids calling it. An example when onPause() is called and not onSaveInstanceState(Bundle) is when activity B is launched in front of activity A: the system may avoid calling onSaveInstanceState(Bundle) on activity A if it isn't killed during the lifetime of B since the state of the user interface of A will stay intact.
Upvotes: 0
Reputation: 25863
In onSaveInstanceState()
you're modifying savedInstanceState
and not saving this modified object. If super
does a copy of your Bundle
, then it will not save this modification.
Try calling super.onSaveInstanceState(savedInstanceState);
at the end of the method instead.
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
String [] a={"haha"};
savedInstanceState.putStringArray("MyStringarray", a);
super.onSaveInstanceState(savedInstanceState);
Toast.makeText(context, "Saved array", Toast.LENGTH_SHORT).show();
}
Upvotes: 4