我有一個計時器列表,如果我旋轉他們被刪除的屏幕。我試過下面的代碼,但它崩潰了。我認爲保存/恢復格式存在問題。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 –