Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store values in onSaveInstanceState() and retrive?

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();
}
like image 820
Mohan Raj Avatar asked Jun 19 '13 05:06

Mohan Raj


People also ask

How do you save data on onSaveInstanceState?

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.

What is the use of onSaveInstanceState () method?

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.

When onSaveInstanceState () is called?

Note that onSaveInstanceState() is called when your activity goes into the background and NOT when the app process is about to be killed.

How do you save state on onPause?

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


2 Answers

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

like image 120
Oleksii K. Avatar answered Sep 29 '22 17:09

Oleksii K.


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");
  }
like image 29
abhi Avatar answered Sep 29 '22 17:09

abhi