问题描述
我想将数据保存在我的活动,比恢复。我保存的数据的onSaveInstanceState()
,然后我试图恢复 onRestoreInstanceState数据()
。
I am trying to save data in my activity and than restore it.I save data in on onSaveInstanceState()
and then I try to restore the data in onRestoreInstanceState()
.
我设置断点,该方法的onSaveInstanceState()
被调用。但 onRestoreInstanceState()
或的onCreate()
从来没有。
I setup breakpoint, the method onSaveInstanceState()
get called. But onRestoreInstanceState()
or onCreate()
never did.
下面是我的步骤:
- 在开始我的
活动
。 - preSS'家'的手机上的按钮。
的onSaveInstanceState()
被调用。 - 单击在发射器的图标和再次启动我的
活动。
- start my
Activity
. - press 'Home' button on the phone.
onSaveInstanceState()
get called. - Click the icon in the launcher and launch my
Activity
again.
在这个时候,只有 onRestart()
被调用。但是,并非 onRestoreInstanceState()
或的onCreate()
。
At this time, only onRestart()
get called. But not onRestoreInstanceState()
or onCreate()
.
有谁知道为什么吗?
推荐答案
那么,如果 onRestart()
被调用时,实例变量的值将由维护应用程序栈本身,因此你不需要恢复它们。
Well, if onRestart()
is called, the value of the instance variables would be maintained by the application stack itself and thus you do not need to restore them.
的onCreate()
只调用了,当你的活动
的的onStop()
被调用,进程被杀死。
onCreate()
method is only called when your Activity
's onStop()
is called and the process is killed.
请参照活动
生命周期中的获得一个清晰的认识。
Please refer the Activity
life cycle Android Activity Life Cycle for a clear understanding.
您可能要检查的onStop()
方法是否被调用,如果你的进程被终止。我不认为你的过程中被杀害,你所描述的情景。
You may want to check whether the onStop()
method is called and if your process is killed. I do no think that your process gets killed by the scenario which you have described.
在 onRestoreInstanceState()
方法是非常棘手的。我不知道什么时候正是它叫,但我看到了,而从Potrait改变为横向,它被称为一次。
the onRestoreInstanceState()
method is very tricky. I do not know when exactly it is called but I saw it was called once while changing from Potrait to Landscape.
这篇关于为什么onRestoreInstanceState()不会被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!