我是 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()));
}