Am trying to store value of a arraylist in onSavedInstanceState()
and get the values in a array list in onCreate method but it raises an eeror like unable to pause activity
here is my code
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putParcelable("Old", (Parcelable) profileDetails );
super.onSaveInstanceState(savedInstanceState);
}
and in my onCreate()
if (savedInstanceState != null) {
profileDetails= (ArrayList<ProfileDetails>)savedInstanceState.getParcelable("Old");
}
else {
profileDetails = GetSearchResults();
}
You need to override onSaveInstanceState(Bundle savedInstanceState) and write the application state values you want to change to the Bundle parameter like this: @Override public void onSaveInstanceState(Bundle savedInstanceState) { super.
The onSaveInstanceState() callback stores data needed to reload the state of a UI controller, such as an activity or a fragment, if the system destroys and later recreates that controller. To learn how to implement saved instance state, see Saving and restoring activity state in the Activity Lifecycle guide.
Note that onSaveInstanceState() is called when your activity goes into the background and NOT when the app process is about to be killed.
Before your app is suspended, the onSaveInstanceState() method of your Activity will be fired, followed by onPause() . You should place code in onSaveInstanceState() that saves any UI state that you don't want to lose. Other data, that does not relate to UI state, should be saved in onPause() .
Bundle can store Serializable
objects too.
Make sure than your class ProfileDetails implements Serializable
.
After that you will be able to save/restore ArrayList in Bundle using:
savedInstanseState.putSerializable("Old", profileDetails);
...
profileDetails = (ArrayList<ProfileDetails>)savedInstanceState.getSerializable("Old");
Also you should guarantee that class ProfileDetails
contains only fields of primitive types or Serializable (Strings, Arrays already implements this interface).
to save value in save instance state
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Save UI state changes to the savedInstanceState.
// This bundle will be passed to onCreate if the process is
// killed and restarted.
savedInstanceState.putBoolean("MyBoolean", true);
savedInstanceState.putDouble("myDouble", 1.9);
savedInstanceState.putInt("MyInt", 1);
savedInstanceState.putString("MyString", "Welcome back to Android");
// etc.
super.onSaveInstanceState(savedInstanceState);
}
to restore value
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// Restore UI state from the savedInstanceState.
// This bundle has also been passed to onCreate.
boolean myBoolean = savedInstanceState.getBoolean("MyBoolean");
double myDouble = savedInstanceState.getDouble("myDouble");
int myInt = savedInstanceState.getInt("MyInt");
String myString = savedInstanceState.getString("MyString");
}
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