onSaveInstanceState() 和 onRestoreInstanceState() [英] onSaveInstanceState () and onRestoreInstanceState ()
问题描述
我正在尝试使用 onSaveInstanceState()
和 onRestoreInstanceState()
方法保存和恢复 Activity
的状态.>
问题在于它从不进入 onRestoreInstanceState()
方法.谁能向我解释这是为什么?
通常你在 onCreate()
中恢复你的状态.也可以在 onRestoreInstanceState()
中恢复它,但不是很常见.(onRestoreInstanceState()
在 onStart()
之后调用,而 onCreate()
在 onStart()
之前调用.
使用 put 方法在 onSaveInstanceState()
中存储值:
protected void onSaveInstanceState(Bundle icicle) {super.onSaveInstanceState(冰柱);icicle.putLong("param", value);}
并恢复onCreate()
中的值:
public void onCreate(Bundle icicle) {如果(冰柱!= null){value = icicle.getLong("param");}}
I'm trying to save and restore the state of an Activity
using the methods onSaveInstanceState()
and onRestoreInstanceState()
.
The problem is that it never enters the onRestoreInstanceState()
method. Can anyone explain to me why this is?
Usually you restore your state in onCreate()
. It is possible to restore it in onRestoreInstanceState()
as well, but not very common. (onRestoreInstanceState()
is called after onStart()
, whereas onCreate()
is called before onStart()
.
Use the put methods to store values in onSaveInstanceState()
:
protected void onSaveInstanceState(Bundle icicle) {
super.onSaveInstanceState(icicle);
icicle.putLong("param", value);
}
And restore the values in onCreate()
:
public void onCreate(Bundle icicle) {
if (icicle != null){
value = icicle.getLong("param");
}
}
这篇关于onSaveInstanceState() 和 onRestoreInstanceState()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!