How to save custom ArrayList on Android screen?

I have an ArrayList with custom objects that I would like to save and restore on the screen.

I know this can be done using onSaveInstanceState and onRestoreInstanceState if I have to make ArrayList my own class that implements either Parcelable or Serializable ... But is there a way to do this without creating another class?

+45
android object arraylist parcelable screen-rotation
Sep 19 '12 at 22:34
source share
3 answers

You do not need to create a new class to pass the ArrayList of your custom objects. You should simply implement the Parcelable class for your object and use Bundle # putParcelableArrayList () in onSaveInstanceState() and onRestoreInstanceState() . This method will store ArrayList Parcelables on its own.




Since the subject of Parcelables (and Serializables and Bundles) sometimes makes my head hurt, here is a basic ArrayList example containing custom Parcelable objects stored in a Bundle. (It is cut and pasted into a stack, without a layout.)

Parcelable implementation

 public class MyObject implements Parcelable { String color; String number; public MyObject(String number, String color) { this.color = color; this.number = number; } private MyObject(Parcel in) { color = in.readString(); number = in.readString(); } public int describeContents() { return 0; } @Override public String toString() { return number + ": " + color; } public void writeToParcel(Parcel out, int flags) { out.writeString(color); out.writeString(number); } public static final Parcelable.Creator<MyObject> CREATOR = new Parcelable.Creator<MyObject>() { public MyObject createFromParcel(Parcel in) { return new MyObject(in); } public MyObject[] newArray(int size) { return new MyObject[size]; } }; } 

Save / Restore Status

 public class Example extends ListActivity { ArrayList<MyObject> list; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if(savedInstanceState == null || !savedInstanceState.containsKey("key")) { String[] colors = {"black", "red", "orange", "cyan", "green", "yellow", "blue", "purple", "magenta", "white"}; String[] numbers = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"}; list = new ArrayList<MyObject>(); for(int i = 0; i < numbers.length; i++) list.add(new MyObject(numbers[i], colors[i])); } else { list = savedInstanceState.getParcelableArrayList("key"); } setListAdapter(new ArrayAdapter<MyObject>(this, android.R.layout.simple_list_item_1, list)); } @Override protected void onSaveInstanceState(Bundle outState) { outState.putParcelableArrayList("key", list); super.onSaveInstanceState(outState); } } 
+78
Sep 19 '12 at 10:40
source share

You can use onRetainNonConfigurationInstance() . It allows you to save any object before changing the configuration and restore it after getLastNonConfigurationInstanceState ().

Inside the action:

  @Override public Object onRetainNonConfigurationInstance() { return myArrayList; } 

Inside onCreate() :

  try{ ArrayList myArrayList = (ArrayList)getLastNonConfigurationInstance(); } catch(NullPointerException e) {} 

Processing Runtime Changes: http://developer.android.com/guide/topics/resources/runtime-changes.html Documentation: http://developer.android.com/reference/android/app/Activity.html#onRetainNonConfigurationInstance% 28% 29

+3
Sep 20
source share

Yes, you can save the compound object in the general settings. Let them talk:

 Student mStudentObject = new Student(); SharedPreferences appSharedPrefs = PreferenceManager .getDefaultSharedPreferences(this.getApplicationContext()); Editor prefsEditor = appSharedPrefs.edit(); Gson gson = new Gson(); String json = gson.toJson(mStudentObject); prefsEditor.putString("MyObject", json); prefsEditor.commit(); 

and now you can get your object as:

  SharedPreferences appSharedPrefs = PreferenceManager .getDefaultSharedPreferences(this.getApplicationContext()); Editor prefsEditor = appSharedPrefs.edit(); Gson gson = new Gson(); String json = appSharedPrefs.getString("MyObject", ""); Student mStudentObject = gson.fromJson(json, Student.class); 
-2
Jul 06 '15 at 11:37
source share



All Articles