getSavedInstanceState()和onViewStateRestored()bundle

时间:2016-01-22 05:01:23

标签: java android android-fragments android-orientation

我在Activity中有一个片段,当旋转设备时,会调用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);

2 个答案:

答案 0 :(得分:2)

您不应自己手动创建状态包,使用已在方法onSaveInstanceState()中传递的outState对象并在onCreate()方法中恢复。另外,你应该在片段的onCreate()中调用setRetainInstance(true)。

答案 1 :(得分:0)

我怀疑它与您的方法签名有关。你为什么使用static?静态方法无法访问实例成员。这样:

public static Bundle getSavedInstanceState()
public static void restoreInstanceState(Bundle savedInstanceState)

应该是:

public Bundle getSavedInstanceState()
public void restoreInstanceState(Bundle savedInstanceState)

其次,您的字段名称非常类似于Java。这样:

BTN_startValueDown

应该是(例如):

btnStartValueDown

首先修改

应该早点想到这个。由于您只是想保留片段的状态,只需在片段setRetainInstance(true)中调用onCreate()即可。请参阅here

第二次修改

另一个回答者是对的:你不应该为外出状态创建另一个Bundle。如果您真的想在单独的方法中创建该Bundle,请改用:

outState.putAll(getSavedInstanceState());