我有一个计时器列表,如果我旋转他们被删除的屏幕。我试过下面的代码,但它崩溃了。我认为保存/恢复格式存在问题。Android保存并恢复ArrayList <Long>屏幕更改
ArrayList<Long> timesList = new ArrayList<Long>(); // List of partial times in milliseconds
/** Save state while screen orientation changes */
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
// Save UI state changes to the savedInstanceState.
// This bundle will be passed to onCreate if the process is
// killed and restarted.
savedInstanceState.putSerializable("PartialTimes", timesList);
}
/** Restore state while screen orientation changes */
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// Restore UI state from the savedInstanceState.
// This bundle has also been passed to onCreate.
timesList = (ArrayList<Long>) savedInstanceState.getSerializable("PartialTimes");
}
还要确保你没有重新创建'ArrayList'每次。也就是说,你不应该在方向改变时创建一个新的ArrayList –
我在onCreated方法之前创建它(刚刚导入之后) – avafab
另外,作为一个附注,使你的对象'Parcelable'比Android上的序列化更快。请参阅http://stackoverflow.com/questions/3611843/is-using-serializable-in-android-bad –