activity life cycle Android

会有一股神秘感。 提交于 2019-12-13 14:12:26

问题


I have read several posts on here and elsewhere about the life cycle but I'm still confused. What do we store in the onpause, how do we store it and how do you recall it in the onresume Method. Any more info and if possible a detailed example would be greatly appreciated.


回答1:


Is this what you mean

For further explanation

  • onCreate(Bundle) is where you initialize your activity. Most importantly, here you will usually call setContentView(int) with a layout resource defining your UI, and using findViewById(int) to retrieve the widgets in that UI that you need to interact with programmatically.
  • onPause() is where you deal with the user leaving your activity. Most importantly, any changes made by the user should at this point be committed (usually to the ContentProvider holding the data).
  • onResume() Called after onRestoreInstanceState(Bundle), onRestart(), or onPause(), for your activity to start interacting with the user. This is a good place to begin animations, open exclusive-access devices (such as the camera), etc.
  • onDestroy() Perform any final cleanup before an activity is destroyed. This can happen either because the activity is finishing (someone called finish() on it, or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the isFinishing() method.

You can see it all here . Hope it helps.




回答2:


Depends on what you are using. Things like OpenGL and the camera app will require you take certain action whenever the application life-cycle changes. For example, resources need to be freed when on pause is called, then u allocate them again when on resume is called.



来源:https://stackoverflow.com/questions/19484493/activity-life-cycle-android

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