[英]state of app after 2 rotations
i have been through this documentation regarding the topic save a state before the foreground activity will be destroyed... 我已经阅读过有关本主题的文档,在前景活动将被销毁之前保存状态...
and everything works really good now (after a device rotation), but when i rotate my device again after a rotation, i will loose my data again :( 并且现在一切正常(在设备旋转之后),但是当我在旋转之后再次旋转设备时,我将再次丢失数据:(
here is my code 这是我的代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final MainActivity activity = this;
activity.setTitle("Cow Counter");
TextView QntyResultField = findViewById(R.id.textView);
QntyResultField.setText(Integer.toString(cowQnty));
}
// invoked when the activity may be temporarily destroyed, save the instance state here
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("qnty", cowQnty);
}
// How we retrieve the data after app crash...
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
//cowQnty = savedInstanceState.getInt("qnty");
TextView QntyResultField = findViewById(R.id.textView);
QntyResultField.setText("Cows: "+Integer.toString(savedInstanceState.getInt("qnty")));
}
I think the solution will be maybe to implement a check if an instance state was already restored before... 我认为解决方案可能是实施检查实例状态是否已在...之前恢复的...
i have tried then this here: 我已经在这里尝试过了:
if(savedInstanceState.getInt("qnty") != 0){
TextView QntyResultField = findViewById(R.id.textView);
QntyResultField.setText("Cows: "+Integer.toString(savedInstanceState.getInt("qnty")));
}
buit then my inital part in my onCreate() method will write a zero in my result field 然后我的onCreate()方法的初始部分将在结果字段中写入零
TextView QntyResultField = findViewById(R.id.textView);
QntyResultField.setText(Integer.toString(cowQnty));
Could anyone tell me if I am close to the solution? 谁能告诉我是否接近解决方案?
You use a variable called cowQnty
to store the value that is then saved in the bundle for your onSaveInstanceState
as outState.putInt("qnty", cowQnty);
您使用一个名为
cowQnty
的变量来存储该值,然后将其保存在onSaveInstanceState
捆绑软件中,作为outState.putInt("qnty", cowQnty);
, then when you restore it in onRestoreInstanceState
you only set the TextView
's value to the retrieved value and do not update the value for cowQnty
. 然后在
onRestoreInstanceState
还原它时,只需将TextView
的值设置为检索到的值,而不更新cowQnty
的值。
How do you expect then to save an empty field again? 您如何期望再次保存一个空字段? There are two solutions to this;
有两种解决方案;
Firstly, if cowQnty
is not a sizeable amount and you do not mind using a tad of RAM, make cowQnty
a static
field and it will persist the data without needing to save it in a Bundle
at all. 首先,如果
cowQnty
不是很大的数量,并且您不介意使用一点RAM, cowQnty
static
字段,它将持久保存数据而根本不需要将其保存在Bundle
中。
Secondly, just set cowQnty
's value once again when you restore your state (why did you comment it out??), like so: 其次,只需在恢复状态时再次设置
cowQnty
的值即可(为什么将其注释掉?),如下所示:
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
cowQnty = savedInstanceState.getInt("qnty");
TextView QntyResultField = findViewById(R.id.textView);
QntyResultField.setText("Cows: "+Integer.toString(savedInstanceState.getInt("qnty")));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.