我在Activity中有一个Fragment,当旋转设备时,将调用onSaveInstanceState()和onViewStateRestored()。
onSaveInstanceState():
@Override
public void onSaveInstanceState(Bundle outState){
Log.e("Fragment", "Called onSaveInstanceState");
outState = getSavedInstanceState();
super.onSaveInstanceState(outState);
}
这是在onSaveInstanceState()中设置outState捆绑包的原因:
public static Bundle getSavedInstanceState(){
Bundle saveState = new Bundle();
//Save view states
saveState.putBoolean("startButtonVisible", buttonStart.getVisibility() == View.VISIBLE);
saveState.putBoolean("stopButtonVisible", buttonStop.getVisibility() == View.VISIBLE);
saveState.putBoolean("resetButtonVisible", buttonReset.getVisibility() == View.VISIBLE);
saveState.putBoolean("saveButtonVisible", buttonSave.getVisibility() == View.VISIBLE);
saveState.putBoolean("openLatestFileButtonVisible", buttonOpenLatestFile.getVisibility() == View.VISIBLE);
saveState.putBoolean("editTextStartValueVisible", editTextStartValue.isEnabled());
saveState.putBoolean("buttonStartValueDownVisible", buttonStartValueDown.isEnabled());
saveState.putBoolean("buttonStartValueUpVisible", buttonStartValueUp.isEnabled());
saveState.putBoolean("editTextEndValueVisible", editTextEndValue.isEnabled());
saveState.putBoolean("buttonEndValueDownVisible", buttonEndValueDown.isEnabled());
saveState.putBoolean("buttonEndValueUpVisible", buttonEndValueUp.isEnabled());
return saveState;
}
这样可以正确保存数据,但是在调用onViewStateRestored()时,数据不正确。
@Override
public void onViewStateRestored(Bundle savedInstanceState){
Log.e("Fragment", "Called onViewStateRestored");
super.onViewStateRestored(savedInstanceState);
//Restore view states
if (savedInstanceState != null){
restoreInstanceState(savedInstanceState);
if (buttonReset.getVisibility() == View.VISIBLE){
updateUi();
}
}
}
调用onViewStateRestored()时,它还会调用restoreInstanceState(),它应该还原每个视图的状态。
public static void restoreInstanceState(Bundle savedInstanceState){
buttonStart.setVisibility(savedInstanceState.getBoolean("startButtonVisible") ? View.VISIBLE : View.INVISIBLE);
buttonStop.setVisibility(savedInstanceState.getBoolean("stopButtonVisible") ? View.VISIBLE : View.INVISIBLE);
buttonReset.setVisibility(savedInstanceState.getBoolean("resetButtonVisible") ? View.VISIBLE : View.INVISIBLE);
buttonSave.setVisibility(savedInstanceState.getBoolean("saveButtonVisible") ? View.VISIBLE : View.INVISIBLE);
buttonOpenLatestFile.setVisibility(savedInstanceState.getBoolean("openLatestFileButtonVisible") ? View.VISIBLE : View.INVISIBLE);
editTextStartValue.setEnabled(savedInstanceState.getBoolean("editTextStartValueVisible"));
buttonStartValueDown.setEnabled(savedInstanceState.getBoolean("buttonStartValueDownVisible"));
buttonStartValueDown.setImageResource(buttonStartValueDown.isEnabled() ? R.drawable.ic_arrow_down : R.drawable.ic_arrow_down_gray);
buttonStartValueUp.setEnabled(savedInstanceState.getBoolean("buttonStartValueUpVisible"));
buttonStartValueUp.setImageResource(buttonStartValueUp.isEnabled() ? R.drawable.ic_arrow_up : R.drawable.ic_arrow_up_gray);
editTextEndValue.setEnabled(savedInstanceState.getBoolean("editTextEndValueVisible"));
buttonEndValueDown.setEnabled(savedInstanceState.getBoolean("buttonEndValueDownVisible"));
buttonEndValueDown.setImageResource(buttonEndValueDown.isEnabled() ? R.drawable.ic_arrow_down : R.drawable.ic_arrow_down_gray);
buttonEndValueUp.setEnabled(savedInstanceState.getBoolean("buttonEndValueUpVisible"));
buttonEndValueUp.setImageResource(buttonEndValueUp.isEnabled() ? R.drawable.ic_arrow_up : R.drawable.ic_arrow_up_gray);
Log.e("Fragment", "Restored view visibility");
}
为什么在调用onViewStateRestored()时数据不正确?无论以前是什么,所有视图都变得不可见。保存和还原状态时,捆绑包是否相同?
但是,当调用
fragmentManager.beginTransaction().replace(R.id.content_frame, CustomFragmentManager.savedFilesFragment).commit();
然后手动调用onViewStateRestored()时,所有这些代码均有效。编辑:更改的字段名称。
解决方案:我已将
outState = new Bundle(getSavedInstanceState());
替换为outState.putAll(getSavedInstanceState());
,并且现在可以使用了。无需调用setRetainInstance(true);
。 最佳答案
您不应该使用在onSaveInstanceState()方法中传递并在onCreate()方法中恢复的outState对象自己手动创建状态包。另外,您应该在片段的onCreate()中调用setRetainInstance(true)。