Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>First, you need to determine what is actually the "state" in your app. You haven't said what you are actually doing, but let me assume that the ArrayList of objects is the state the user is working with.</p> <p>Second, decide what the lifecycle of this state actually is. Is it really tied to that activity? Or should the user not lose it if say their battery runs low, the device turns off, and they later return to your app? If the former, <code>onSaveInstanceState()</code> is correct; if the latter, you'll want to save to persistent storage in <code>onPause()</code>.</p> <p>For <code>onSaveInstanceState()</code> with custom objects, the key is to implement the <code>Parcelable</code> interface. This involves implementing the methods on Parcelable, as well as making a static CREATOR object in your class. Here's the code for a typical simple Parcelable class:</p> <p><a href="https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/content/ComponentName.java" rel="nofollow noreferrer">https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/content/ComponentName.java</a></p> <p>The key functions are the Parcelable implementation:</p> <p><a href="https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/content/ComponentName.java#317" rel="nofollow noreferrer">https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/content/ComponentName.java#317</a></p> <p>and the CREATOR static class:</p> <p><a href="https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/content/ComponentName.java#355" rel="nofollow noreferrer">https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/content/ComponentName.java#355</a></p> <p>(The static <code>writeToParcel()</code> and <code>readFromParcel()</code> are just conveniences that were done for that class and not required.)</p> <p>Now that you have that, you can put your entire ArrayList of objects into the saved state Bundle with <code>Bundle.putParcelableArrayList</code>:</p> <p><a href="http://developer.android.com/reference/android/os/Bundle.html#putParcelableArrayList%28java.lang.String,%20java.util.ArrayList%3C?%20extends%20android.os.Parcelable%3E%29" rel="nofollow noreferrer">http://developer.android.com/reference/android/os/Bundle.html#putParcelableArrayList(java.lang.String, java.util.ArrayList)</a></p> <p>In <code>Activity.onCreate()</code>, check to see if you have a <code>savedState Bundle</code>, and if so try to retrieve the ArrayList from that and use it if found, creating a new adapter and list view for the new activity that are used to display it.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload