我在使用 onRestoreInstanceState() 方法时遇到了一些问题。这个 SumWindow 活动向用户显示了一些问题,并将用户的分数存储在变量 try 和 rights 中。问题:如果用户按下回,然后从主菜单用户按下“播放!” (SumWindow 活动创建的地方)分数被重置为 0 分(满分)。我确定我错过了这个保存/加载的基本内容。这是我的代码(为便于阅读而简化)
public class SumWindow extends Activity implements OnClickListener {
int tries;
int rights;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
tries = savedInstanceState.getInt("tries");
right = savedInstanceState.getInt("right");
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putInt("tries", tries);
savedInstanceState.putInt("rights", rights);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
tries = savedInstanceState.getInt("tries");
right = savedInstanceState.getInt("rights");
}