我正在尝试将数组列表的值存储在 onSavedInstanceState()
中,并在 onCreate 方法中获取数组列表中的值,但它会引发错误,例如无法暂停 Activity
这是我的代码
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putParcelable("Old", (Parcelable) profileDetails );
super.onSaveInstanceState(savedInstanceState);
}
在我的 onCreate()
if (savedInstanceState != null) {
profileDetails= (ArrayList<ProfileDetails>)savedInstanceState.getParcelable("Old");
}
else {
profileDetails = GetSearchResults();
}
最佳答案
Bundle 也可以存储Serializable
对象。
确保您的类 ProfileDetails 实现了 Serializable
。
之后,您将能够使用以下方法在 Bundle 中保存/恢复 ArrayList:
savedInstanseState.putSerializable("Old", profileDetails);
...
profileDetails = (ArrayList<ProfileDetails>)savedInstanceState.getSerializable("Old");
另外你应该保证 class ProfileDetails
只包含基本类型或 Serializable 的字段(字符串,数组已经实现了这个接口(interface))。
关于android - 如何在 onSaveInstanceState() 中存储值并检索?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17183432/