我的代码是这样的:
@Override
public Parcelable onSaveInstanceState() {
Parcelable superState = super.onSaveInstanceState();
SavedState savedState = new SavedState(superState);
savedState.progress = mProgress;
return savedState;
}
@Override
public void onRestoreInstanceState(Parcelable state) {
SavedState savedState = (SavedState) state;
mProgress = savedState.progress;
super.onRestoreInstanceState(savedState.getSuperState());
}
当mProgress
传递给savedState
中的onSaveInstanceState
并且onRestoreInstanceState
中获得0时,savedState.mProgress = mProgress
值为60。
如果我修改代码
在onSavedInstanceState
到savedState.mProgress = 10
onRestoreInstanceState
{}} {} mProgress
{/ 1}}。{/ 1}}
太奇怪了。如果我使OrderedDict
静态然后我得到60.我不知道是什么问题。
答案 0 :(得分:1)
像这样使用,
@Override
protected void onCreate(Bundle savedInstanceState) {
if (savedInstanceState != null) {
mProgress = savedInstanceState.getInt("progress");
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("progress", mProgress);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
if (savedInstanceState != null) {
mProgress = savedInstanceState.getInt("progress");
}
}