如何在Android屏幕上保存自定义的ArrayList旋转?
我有一个ArrayList
与自定义对象,我希望能够保存和恢复在屏幕上旋转。
我知道这可以通过onSaveInstanceState
和onRestoreInstanceState
完成,如果我要使ArrayList
自己的类,实现Parcelable
或Serializable
…但有没有办法做到这一点,而不创build另一个类?
您不需要创build一个新类来传递自定义对象的ArrayList。 您应该简单地为对象实现Parcelable类,并在onSaveInstanceState()
和onRestoreInstanceState()
使用Bundle# onRestoreInstanceState()
。 这个方法将自己存储一个Parcelables的ArrayList。
因为可接受对象(和可序列化和捆绑)的主题有时会使我的头受伤,下面是包含存储在捆绑包中的自定义Parcelable对象的ArrayList的基本示例。 (这是剪切和粘贴可运行,不需要布局。)
实现Parcelable
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]; } }; }
保存/恢复状态
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); } }
你可以使用onRetainNonConfigurationInstance()
。 它允许您在configuration更改之前保存任何对象,并在使用getLastNonConfigurationInstanceState
()之后恢复它。
活动内部:
@Override public Object onRetainNonConfigurationInstance() { return myArrayList; }
在onCreate()
里面:
try{ ArrayList myArrayList = (ArrayList)getLastNonConfigurationInstance(); } catch(NullPointerException e) {}
处理运行时更改: http : //developer.android.com/guide/topics/resources/runtime-changes.html文档: http : //developer.android.com/reference/android/app/Activity.html#onRetainNonConfigurationInstance%28 %29
是的,您可以将复合对象保存在共享首选项中。 我们说:
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();
现在你可以检索你的对象为:
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);