问题描述
我试图保存和恢复一个活动的状态
使用方法的onSaveInstanceState()
和 onRestoreInstanceState()
。
I'm trying to save and restore the state of an Activity
using the methods onSaveInstanceState()
and onRestoreInstanceState()
.
现在的问题是,它不会进入 onRestoreInstanceState()
方法。谁能给我解释一下这是为什么?
The problem is that it never enters the onRestoreInstanceState()
method. Can anyone explain to me why this is?
推荐答案
通常你恢复的onCreate您的状态()
。这是可能给它的 onRestoreInstanceState恢复()
为好,但不是很普遍。 ( onRestoreInstanceState()
在 ONSTART被称为()
,而的onCreate()
在 ONSTART被称为()
。
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()
.
使用看跌期权的方法来存储值的onSaveInstanceState()
:
Use the put methods to store values in onSaveInstanceState()
:
protected void onSaveInstanceState(Bundle icicle) {
super.onSaveInstanceState(icicle);
icicle.putLong("param", value);
}
而在的onCreate还原值()
:
public void onCreate(Bundle icicle) {
if (icicle != null){
value = icicle.getLong("param");
}
}
您不必存储视图状态,因为它们会自动存储。
You do not have to store view states, as they are stored automatically.
这篇关于的onSaveInstanceState()和onRestoreInstanceState()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!