安卓活动状态
我有几个关于 android 中活动的变量/状态的快速问题。当您访问该活动的选项(首选项)并返回活动时,该活动会实现“onResume()”,不是吗?
另一个问题:当一个 Activity 处于“onPause()”状态时,它的变量会消失吗?还是它保持状态?
谢谢
I have a couple of quick question about the variable/state of an activity in android. When you access to the options of that activity (Preferences) and you come back to activity, that activity realize "onResume()" isnt it?
And another question: when one activity is "onPause()" state, does its variables die? Or does it keep the state?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个很难回答的问题,因为 Android 操作系统决定这些变量何时被销毁。它在 Android 应用程序生命周期中得到了更好的定义。变量可能会在 onPause 之后立即丢失或根本不丢失。 (因此这不是 onPause() 所做的)。人们可能会认为 OnDestroy() 已经全部完成,但事实并非如此。Android 可以在不调用 OnDestor 的情况下杀死所有变量。因此,一般来说,最好使用 onSaveInstanceState 保存状态并使用 onRestoreInstanceState 恢复状态。
It's a difficult question because the Android OS decides when those variables are detroyed. It's better defined in the Android Application Lifecycle.. variables could be lost immediatley after onPause or not at all. (thus it's no onPause()'s doing). One would think it's all done OnDestroy() but that's not the case either.. Android could kill all variables w/o OnDestor ever being called. So in general it's best to save state using onSaveInstanceState and restore it using onRestoreInstanceState.