开发者

Android onStop/onDestroy - when might these be used?

开发者 https://www.devze.com 2023-04-01 03:01 出处:网络
Looking at the Activity Life Cycle diagram, I notice that onPause() and onStop() can both lead to the \"process\" being killed. This would require onCreate() to be called when the user wants to resume

Looking at the Activity Life Cycle diagram, I notice that onPause() and onStop() can both lead to the "process" being killed. This would require onCreate() to be called when the user wants to resume their application. The point being that onSt开发者_运维百科op() is not necessarily called, nor is onDestroy(), but that onPause() may be the only event that the Activity may see. This being the case, onPause() must handle saving the application status so that the user can return back to it later, regardless of whether onStop() is called or not.

I can see onDestroy() being used to clean up Activity specific resources that would naturally be eliminated in a process kill action. Is there anything else that onDestroy() would be good for?

And what would onStop() be good for? Why would I want to override it for?


If I got your question right: It depends what you want to do with your application. Let's say you are programming application that uses GPS. In the onStop() which is called when the activity is no longer visible to the user, you can remove these requests. Or you can stop the some service if your application is running any. Or you can save preferences (not recommended, do it in onPause() instead), or you can close permanent connection to a server.....If I think of anything else, I'll add more...


If you have read the doc further, you'll see following:

Saving activity state

The introduction to Managing the Activity Lifecycle briefly mentions that when an activity is paused or stopped, the state of the activity is retained. This is true because the Activity object is still held in memory when it is paused or stopped—all information about its members and current state is still alive. Thus, any changes the user made within the activity are retained in memory, so that when the activity returns to the foreground (when it "resumes"), those changes are still there.

However, when the system destroys an activity in order to recover memory, the Activity object is destroyed, so the system cannot simply resume it with its state intact. Instead, the system must recreate the Activity object if the user navigates back to it. Yet, the user is unaware that the system destroyed the activity and recreated it and, thus, probably expects the activity to be exactly as it was. In this situation, you can ensure that important information about the activity state is preserved by implementing an additional callback method that allows you to save information about the state of your activity and then restore it when the the system recreates the activity.

Summary: After completing onStop() Activity object is still alive in memory. And this will help the system to restore the activity.

Very basic example: consider you are showing your activity to user, and suddenly your friend calls you! Rest you can understand..

So now it it up to you that, which are the resources/objects/connections should be released on which event.


Another example would be to register and unregister a broadcast receiver.

Note that usually these things are placed in onResume and onPause, the difference is subtle though, onResume/onPause are called when the activity gets placed behind another activity, onStart/onStop are called when the activity is no longer visible in the screen.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号