onCreate vs. onResume/onRestart bevhaviour regarding member variables

时光总嘲笑我的痴心妄想 提交于 2021-02-08 13:17:32

问题


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

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

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?


回答1:


  • 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).

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)




回答2:


Suppose a dialogue is initiated from your current activity the main window(Activity) will goes to the onPause State. Once you force activity to be in background(Suppose you press home button) The Activity will goes to onPause State.



来源:https://stackoverflow.com/questions/16058461/oncreate-vs-onresume-onrestart-bevhaviour-regarding-member-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!