关于成员变量的 onCreate 与 onResume/onRestart 行为 [英] onCreate vs. onResume/onRestart bevhaviour regarding member variables

查看:85
本文介绍了关于成员变量的 onCreate 与 onResume/onRestart 行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我打开一个活动时,我知道我可以在 onCreate 函数中初始化内容.

When I open an activity I know that I can initialize stuff in the onCreate function.

但是 OnResume 和 onRestart 函数的行为是什么?什么时候调用这些函数?

But what is the behaviour on the OnResume and onRestart function? When are these functions called?

具体来说:我在 onCreate 函数中初始化了一个局部成员变量 auiqring 对全局对象的引用.现在,当用户被打断时,例如被一个电话打断,活动可以被关闭.后来,当用户回到我的视图时,已经初始化的变量的状态是什么?我是否必须重新初始化 onResume/onRestart 函数中的所有内容?那么与 onCreate 相对的功能差异是什么?

Specifically: I initialize a local member variable in the onCreate function auiqring a reference to a global object. Now, when the user is interrupted, for example, by a call, the activity can be closed. Later, when the user comes back to my view, what is the status of the already initiliazed variable? Do I have to reinitialize everything in the onResume/onRestart functions? So what would be the functional difference opposed to onCreate?

推荐答案

  • onCreate:首次启动的活动.您可以在此处初始化您的东西.
  • onResume:用户在另一个活动进入前台后返回活动.(onPause)
  • onRestart:用户在活动不再可见后导航到该活动 (onStop).
    • onCreate: Activity launched for the first time. Here is where you may initialize your stuff.
    • onResume: User returns to the activity after another activity comes into foreground. (onPause)
    • onRestart: User navigates to the activity after it's no longer visible (onStop).
    • 您可以在活动文档上查看完整的生命周期.您的活动内容只会在 onDestroy 被调用时丢失,这会在您完成它时发生,或者当它被系统销毁时(即当具有更高优先级的应用程序需要内存时)

      You can see the complete lifecycle on Activity documentation. Your activity stuff would only be lost when onDestroy is called, which happens when you finish it, or when it's destroyed by the system (i.e. when apps with higher priority need memory)

      这篇关于关于成员变量的 onCreate 与 onResume/onRestart 行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆