问题描述
我指的 http://developer.android.com/reference/安卓/ APP / Activity.html 。
我具有由用户使用,例如它可以中断的活性用户打开菜单调用preferences屏幕。当调用preference屏幕的onSaveInstanceState(捆绑)
叫,我可以节省我的数据。这很好至今。但在pressing后退按钮 onRestoreInstanceState(包savedInstanceState)
终止的不是的调用。
I have an activity which can be "interrupted" by the user, e.g. the user opens the menu to call the preferences screen. When calling the preference screen onSaveInstanceState(Bundle)
is called and I can save my data. That's fine so far. But upon pressing the back button onRestoreInstanceState(Bundle savedInstanceState)
is NOT called.
那么,怎样才能保存我的状态?我必须调用新的活动什么时候做?但如何?
我能想到的唯一途径,是节约国家通过将国家对新的活动,做什么都没有用保存的数据,它返回的第一个活动,并恢复 onActivitResult 。但是,这将意味着,我要传递数据来回刚刚恢复状态。似乎没有成为一个很好的解决方案。
So how can I save my state? Do I have to do it when calling the new activity? But how?
The only way I can think of, is saving the state by passing the state to the new activity, do nothing there with the saved data, return it to the first activity and restore the state in onActivitResult
. But that would mean that I have to pass data back and forth just to restore the state. Doesn't seem to be a nice solution.
推荐答案
可能是一个不好回答,但你肯定
onRestoreInstanceState
需要调用?
Probably a bad answer, but are you sure
onRestoreInstanceState
needs to be called?
捆绑的点和
的onSaveInstanceState
/ 的onCreate
与包/ onRestoreInstanceState
是保存临时数据的一个活动,是在历史堆栈,以防活动必须被杀死,以回收一些内存。如果它被杀死,该活动可以恢复,就好像从来没有死亡,经的onCreate / onRestonreInstanceState
。 的但是,如果该活动没有被杀,可能没有必要恢复其瞬时数据 - psumably $ P $它只是因为它是的
The point of the bundle and
onSaveInstanceState
/ onCreate
with bundle / onRestoreInstanceState
is to save transient data for an activity that is in the history stack, in case the activity must be killed to reclaim some memory. If it is killed, the activity can be restored, as if it were never killed, via onCreate / onRestonreInstanceState
. However, if the activity was not killed, there may be no need to restore its transient data - presumably it is just as it was.
Android的文档谨慎地指出,
onSaveInstanceStae / onRestoreInstanceState
是的的,所以并不能保证在生命周期状态转换为被调用。如果你需要挂接到特定产品过渡,看看在生命周期挂钩方法。例如, onResume
时的活动成为前台活动和的onPause
被调用时,它不再是前台叫活动。
The Android documentation is careful to point out that
onSaveInstanceStae / onRestoreInstanceState
are not lifecycle methods, so aren't guaranteed to be called during lifecycle state transitions. If you need to hook into certain lifecycle transitions, take a look at the lifecycle hook methods. For example, onResume
is called when the activity becomes the foreground activity and onPause
is called when it is no longer the foreground activity.
这篇关于安卓:在Android的生命周期保存状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!