按钮savedInstanceState和onRestoreInstanceState

时间:2019-02-22 03:29:01

标签: android android-7.0-nougat onsaveinstancestate

保存Drawable xml按钮的状态。

private boolean mJam;
private button mSwitch;
private static final String     KEY_ENABLE          = "KEY_ENABLE";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if( null == savedInstanceState ) {
        mJam = false; 
    } else {
        mJam = savedInstanceState.getBoolean(KEY_ENABLE);
    }
   button_layout();

  }

@Override
protected void onSaveInstanceState(Bundle outState) {
    outState.putBoolean(KEY_ENABLE,  mJam);
    outState.putBoolean(KEY_ENABLE, mSwitch.isSelected()); // Saving the state of the
    super.onSaveInstanceState(outState);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    mSwitch.setSelected(savedInstanceState.getBoolean(KEY_ENABLE));
    mJam = savedInstanceState.getBoolean(KEY_ENABLE);
    super.onRestoreInstanceState(savedInstanceState);
}

注意:如果您不想在Android清单中使用ScreenSize,则可以使用以下代码, 如果要手动执行此操作,请继续执行此操作。 PS:在清单中设置屏幕尺寸,如果方向更改,则永远不要进入onCreate,它只是保存了所有实例并按原样显示,当 Android方向更改。

0 个答案:

没有答案