gpt4 book ai didi

java - onSaveInstanceState 在 onCreate 之后没有变

转载 作者:行者123 更新时间:2023-11-30 02:02:34 25 4
gpt4 key购买 nike

嗨,有人能解释一下我做错了什么吗,因为每次我更改屏幕方向时我都无法从 savedInstanceState int "score"获取变量,它会重置变量。

int score;
EditText et;
String etString;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et = (EditText) findViewById(R.id.editText);
if (savedInstanceState != null) {
score = savedInstanceState.getInt("score");
et.setText(savedInstanceState.getString("etText"));
Log.d("TEST2", "score " + score);
} else {
score = 0;
}

}

@Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
super.onSaveInstanceState(outState, outPersistentState);

etString = et.getText().toString();
outState.putString("etText", etString);
outState.putInt("score", score);
Log.d("TEST", "score " + score);
}

public void performAction(View v) {
switch (v.getId()) {
case R.id.button:
score += 1;
break;
case R.id.button2:
Toast.makeText(getApplicationContext(), "Score: " + score, Toast.LENGTH_LONG).show();
break;
}

}

如果您知道解决方案,请提供帮助。

最佳答案

您使用的方法来自 Lollipop,请更改为自 Android 1.0 以来可用的版本

@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);

看到不同的方法签名了吗?

关于java - onSaveInstanceState 在 onCreate 之后没有变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31246661/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com