我有计时器的时间列表,如果我旋转它们被删除的屏幕。我尝试了下面的代码,但它崩溃了。我认为保存/恢复格式存在问题。
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");
}
答案 0 :(得分:1)
只需用于保存
Long[] array = new Long[list.size()];
array = list.toArray(array);
savedInstanceState.putLongArray("PartialTimes", array);
对于反向
ArrayList<Long> list = Arrays.asList(savedInstanceState.getLongArray("PartialTimes"));
答案 1 :(得分:-1)
我终于通过将此行放在主要活动内的清单中来解决问题,这样即使方向发生变化,它也会保留列表值。
android:configChanges="keyboardHidden|orientation"