First, you need to determine what is actually a “state” in your application. You did not say what you are actually doing, but let me assume that the ArrayList is the state the user is working with.
Secondly, determine what the life cycle of this condition really is. Is this really related to this activity? Or should the user not lose it if its battery runs out, the device turns off, and then returns to your application? If the first, onSaveInstanceState() correct; if the latter, you want to store in persistent storage in onPause() .
For onSaveInstanceState() with custom objects, the key must implement the Parcelable interface. This includes implementing methods on Parcelable, as well as creating a static CREATOR object in your class. Here is the code for a typical simple Parcelable class:
https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/content/ComponentName.java
Key features are the implementation of Parcelable:
https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/content/ComponentName.java#317
and the static class CREATOR:
https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/content/ComponentName.java#355
(The static writeToParcel() and readFromParcel() are just the amenities that were made for this class and are not required.)
Now that you have this, you can put your entire ArrayList into the saved Bundle state with the Bundle.putParcelableArrayList :
http://developer.android.com/reference/android/os/Bundle.html#putParcelableArrayList (java.lang.String, java.util.ArrayList)
In Activity.onCreate() , check if you have a savedState Bundle , and if so, try extracting an ArrayList from it and use it if it is found by creating a new adapter and a list view for the new action that is used to display it.
hackbod Oct 12 '10 at 17:05 2010-10-12 17:05
source share