0

在我的应用程序中,我动态设置了线性布局的可见性。问题是在设备旋转时,线性布局的状态(可见或不可见)没有保存。

现在我想通过保存布局的实例状态来保存状态。有人可以帮助我使用相应的方法吗?我必须提供什么价值?

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    // ??
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    // ???
}
4

2 回答 2

0

只需将可见性作为整数存储在 Bundle 中。

private final static String VIEW_1_VISIBILITY = "view_1_visibility";
private View mView1;

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt(VIEW_1_VISIBILITY, mView1.getVisibility());
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    if(savedInstanceState != null){
        mView1.setVisibility(savedInstanceState.getInt(VIEW_1_VISIBILITY, View.VISIBLE));
    }
}
于 2013-08-15T22:25:47.130 回答
0

您可以通过在包中保存布尔值或整数值来保存实例状态:

private static final VISIBILITY_KEY = "anthing you want";

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    boolean isVisible = . . .;
    outState.putBoolean(VISIBILITY_KEY, isVisible);
}

您可以按如下方式读回它:

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    boolean isVisible = savedInstanceState.getBoolean(VISIBILITY_KEY);
    // do something with isVisible
}
于 2013-08-15T22:25:53.523 回答