我有几个片段可以替换另一个片段。这些片段的 UI 发生了变化,我需要保持它的新状态。所以代码看起来很简单:
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
if (mStepTwo == null) {
mStepTwo = new QuizStepTwo();
mStepTwo.setListener(mStepTwoListener);
} else {
fragmentTransaction.remove(mStepTwo);
}
fragmentTransaction.replace(R.id.step_holder, mStepTwo);
fragmentTransaction.addToBackStack("second_step");
fragmentTransaction.commit();
但是,当我用第一步替换第二步时,例如按下后退按钮,它的 UI 状态会回滚到初始状态。
我如何保持状态?OnSaveInstanceState ?或者更舒服的东西?
类似的问题:Android Fragment view state within a tab host , How to restore Android Fragment view state