0

我是 saveInstanceState 的新手。那里有一些很棒的资源,我几乎已经弄清楚了。但是,我的 TextView 会像我想要的那样更新,但它消失了。我希望它在我旋转设备时保留在那里,而不必单击按钮将其恢复。知道我哪里出错了吗?谢谢!

TextView cashView 是我想看到 int cash 的地方。

@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.putInt("cash", cash);
      savedInstanceState.putInt("anteBet", anteBet);
      savedInstanceState.putInt("playBet", playBet);
      savedInstanceState.putInt("pairPlusBet", pairPlusBet);
      savedInstanceState.putString("cashView", cashView.toString());
      // etc.
    }

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
  super.onRestoreInstanceState(savedInstanceState);
  // Restore UI state from the savedInstanceState.
  // This bundle has also been passed to onCreate.
  cash = savedInstanceState.getInt("cash");
  anteBet = savedInstanceState.getInt("anteBet");
  playBet = savedInstanceState.getInt("playBet");
  pairPlusBet = savedInstanceState.getInt("pairPlusBet");
  cashView.setText(savedInstanceState.getString(cashView.toString()));
}
4

1 回答 1

0

哈哈......经过一个小时的摆弄,我一问完问题就明白了。不过感谢您的时间!

@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.putInt("cash", cash);
  savedInstanceState.putInt("anteBet", anteBet);
  savedInstanceState.putInt("playBet", playBet);
  savedInstanceState.putInt("pairPlusBet", pairPlusBet);
  savedInstanceState.putString("cashView", cashView.getText().toString());
  // etc.
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
  super.onRestoreInstanceState(savedInstanceState);
  // Restore UI state from the savedInstanceState.
  // This bundle has also been passed to onCreate.
  cash = savedInstanceState.getInt("cash");
  anteBet = savedInstanceState.getInt("anteBet");
  playBet = savedInstanceState.getInt("playBet");
  pairPlusBet = savedInstanceState.getInt("pairPlusBet");
  cashView.setText(savedInstanceState.getString("cashView"));
}

我改变了 savedInstanceState.putString("cashView", cashView.toString()); 到 savedInstanceState.putString("cashView", cashView.getText().toString());

我改变了 cashView.setText(savedInstanceState.getString(cashView.toString())); 到 cashView.setText(savedInstanceState.getString("cashView"));

不知道是哪一个修复了它,但它是固定的。无论如何谢谢!

于 2013-08-09T20:47:26.183 回答