Android 如何在 onSaveInstanceState 中保存自定义类的实例?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3172333/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to save an instance of a custom class in onSaveInstanceState?
提问by jul
I created an instance of a custom class RestaurantList to hold my data (a list of restaurant data received from a web service as json data).
我创建了一个自定义类 RestaurantList 的实例来保存我的数据(从 Web 服务作为 json 数据接收的餐厅数据列表)。
How can I save it in onSaveInstanceState
?
我怎样才能保存它onSaveInstanceState
?
回答by Maaalte
Custom objects can be saved inside a Bundle when they implement the interface Parcelable
.
Then they can be saved via:
自定义对象在实现接口时可以保存在 Bundle 中Parcelable
。然后可以通过以下方式保存它们:
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelable("key", myObject);
}
Basically the following methods must be implemented in the class file:
基本上必须在类文件中实现以下方法:
public class MyParcelable implements Parcelable {
private int mData;
public int describeContents() {
return 0;
}
/** save object in parcel */
public void writeToParcel(Parcel out, int flags) {
out.writeInt(mData);
}
public static final Parcelable.Creator<MyParcelable> CREATOR
= new Parcelable.Creator<MyParcelable>() {
public MyParcelable createFromParcel(Parcel in) {
return new MyParcelable(in);
}
public MyParcelable[] newArray(int size) {
return new MyParcelable[size];
}
};
/** recreate object from parcel */
private MyParcelable(Parcel in) {
mData = in.readInt();
}
}
回答by dsewtz
I know "that this case is cold", but because i found this thread first, when I was searching for exactly the same thing (and found an answer by now):
我知道“这个案例很冷”,但是因为我首先找到了这个线程,当我搜索完全相同的东西时(并且现在找到了答案):
Imagine Bundle as an XML file.
If you create a new <BUNDLE name="InstanceName" type="ClassName">
you can freely add elements and attributes in a fresh and empty namespace.
把 Bundle 想象成一个 XML 文件。如果你创建一个新的,<BUNDLE name="InstanceName" type="ClassName">
你可以在一个新的空命名空间中自由添加元素和属性。
When onSaveInstance(Bundle outState)
of your MainActivity is called (you can also force this in onPause
), you can create a new: Bundle b = new Bundle();
当onSaveInstance(Bundle outState)
您的 MainActivity 被调用时(您也可以在 中强制调用onPause
),您可以创建一个新的:Bundle b = new Bundle();
Then call your (probably not inherited and not overriden) custom Method onSaveInstance(Bundle b)
in your own class with your newly created Bundle b.
Then (in onSaveInstance(Bundle outState)
) of your MainActivity, call outState.putBundle("StringClassAndInstanceName", b);
然后onSaveInstance(Bundle b)
在您自己的类中使用新创建的 Bundle b调用您的(可能不是继承的和未覆盖的)自定义方法。然后(在onSaveInstance(Bundle outState)
)你的 MainActivity,调用outState.putBundle("StringClassAndInstanceName", b);
When you find this string in onCreate, you can either use a switch/case to recreate this object or (better) have a factory function in your custom class to work with Bundle and "StringClassAndInstanceName".
当您在 onCreate 中找到此字符串时,您可以使用 switch/case 来重新创建此对象,或者(更好)在您的自定义类中有一个工厂函数来处理 Bundle 和“StringClassAndInstanceName”。
回答by Andrew Lee
Custom class objects can be converted to JSON and stored in the bundle as a string. The following example is for Fragments.
自定义类对象可以转换为 JSON 并作为字符串存储在包中。以下示例适用于片段。
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Gson gson = new Gson();
String json= gson.toJson(customClass);
outState.putString("CUSTOM_CLASS", json);
}
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if(savedInstanceState != null) {
String json= savedInstanceState.getString("CUSTOM_CLASS");
if(!json.isEmpty()) {
Gson gson = new Gson();
CustomClass customClass = gson.fromJson(json, CustomClass.class);
}
}
}
For Activities, override onRestoreInstanceStatemethod instead.
对于活动,请改写onRestoreInstanceState方法。