如何保存视图类?

时间:2017-02-19 03:07:43

标签: java android savestate

所以我的应用是这样的: mainActivity - > gameview> gamegeneration

mainActivity会将信息(上下文,属性)传递到gameview,这会将gridColRow金额传递给gamegeneration(生成随机网格),然后会显示gameview

但是,如果我更改了屏幕方向,则会创建一个新网格,即使屏幕方向发生变化,如何保持相同的网格呢?

我在游戏视图中使用它,

protected Parcelable onSaveInstanceState() {}

protected void onRestoreInstanceState(Parcelable state) {} 

&安培;捆绑放置/恢复。

然而它不起作用,网格仍然不同。 下面是一个简单的例子(我只想存储一个数字,但仍有问题)。

感谢

protected Parcelable onSaveInstanceState() {
        Bundle bd = new Bundle();
        bd.putParcelable("instanceState", super.onSaveInstanceState());
        bd.putInt("save1", numbersave);
        return super.onSaveInstanceState();

    }

protected void onRestoreInstanceState(Parcelable state) {
        Bundle bd = (Bundle) state;
        super.onRestoreInstanceState(bd.getParcelable("instanceState"));
        numbersave= bundle.getInt("save1");
    }

1 个答案:

答案 0 :(得分:0)

您需要覆盖onSaveInstanceState(Bundle savedInstanceState)并将要更改的应用程序状态值写入Bundle参数,如下所示:

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
  super.onSaveInstanceState(savedInstanceState);
  // Save UI state changes to the savedInstanceState.
  // This bundle will be passed to onCreate if the process is
  // killed and restarted.
  savedInstanceState.putBoolean("MyBoolean", true);
  savedInstanceState.putDouble("myDouble", 1.9);
  savedInstanceState.putInt("MyInt", 1);
  savedInstanceState.putString("MyString", "Welcome back to Android");
  // etc.
  // Also you can add parcelable and serializable for Objects of classes
}

Bundle本质上是一种存储NVP(“名称 - 价值对”)地图的方式,它将被传递到onCreate(),即你可以在onCraete恢复你的状态

    // Check whether we're recreating a previously destroyed instance
    if (savedInstanceState != null) {
        // Restore UI state from the savedInstanceState.
        // This bundle has also been passed to onCreate.
        boolean myBoolean = savedInstanceState.getBoolean("MyBoolean");
        double myDouble = savedInstanceState.getDouble("myDouble");
        int myInt = savedInstanceState.getInt("MyInt");
        String myString = savedInstanceState.getString("MyString");
        // etc.
        // Also you can get parcelable and serializable for Objects of classes
    } else {
        // Probably initialize members with default values for a new instance
    }

你可以覆盖onRestoreInstanceState(),你可以在这里提取这样的值:

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
  super.onRestoreInstanceState(savedInstanceState);
  // Restore UI state from the savedInstanceState.
  // This bundle has also been passed to onCreate.
  boolean myBoolean = savedInstanceState.getBoolean("MyBoolean");
  double myDouble = savedInstanceState.getDouble("myDouble");
  int myInt = savedInstanceState.getInt("MyInt");
  String myString = savedInstanceState.getString("MyString");
  // etc.
  // Also you can get parcelable and serializable for Objects of classes
}

您通常会使用此技术来存储应用程序的实例值(选择,未保存的文本等)。

修改

要保存班级Gameview对象的状态,班级必须实施parcelable(推荐)或serializable,详情请见answer