我在Activity
:
List<IntManager> mt;
this.mt = new ArrayList<IntManager>();
在mt
列表中我有实现IntManager
接口的对象,但是他们可以说是Point
的类型(我的自定义类)
现在我要在this.mt
中保存onSaveInstanceState (Bundle outState)
并在onRestoreInstanceState
上恢复
我该如何保存? IntManager
必须实施Parcelable
或我的自定义类Point
吗?还是其他什么?
答案 0 :(得分:0)
是的,IntManager
及其使用的所有成员字段都必须实施Parcelable
,以便将其保存在Bundle
中。
答案 1 :(得分:0)
是的,你可以通过实现Parcelable并将它们保存到包
来实现或者您可以使用Json序列化程序将自定义对象列表序列化为json字符串并将字符串保存到bundle中。例如,使用GSon你可以这样做
java.lang.reflect.Type type = new com.google.gson.reflect.TypeToken<List<IntManager>>(){}.getType();
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
String json = new Gson().toJson(mt, type);
outState.putString("message", json);
}
您可以从bundle
恢复instancestateList<IntManager> test=new Gson().fromJson(savedInstanceState.getString("message"), type);
希望这会有所帮助..