简体   繁体   中英

onStop() and onDestroy() might not be called. What good are they?

This has always confused me about the Android lifecycle. In the documentation on Activities , Table 1 and the text below it indicates that onstop() and onDestroy() might never be called. onPause() is the last lifecycle method that is guaranteed to be called.

If onStop() / onDestroy() might not be called, what good are these methods? I recognize where they fall in the lifecycle--I'm not asking about what they signal, I'm wondering what kind of code belongs in them if you can't count on them being called. Other posts asking about the utility of these methods say you should use them to free up resources--would this not be unreliable and bug-prone, as those methods may never be called?

I could imagine something like if the app is killed and those methods aren't called, the process itself is killed so any resource pointers you have might be garbage collected or otherwise cleaned up for you. In that case, onStop() / onDestroy() might be responsible for cleaning up resources that would be held onto until the process is killed. That seems a bit convoluted, however.

Does anyone have any examples of how onStop() / onDestroy() can be used correctly and safely, without introducing subtle bugs or resource leaks depending on how Android decides to shut down the Activity?

I could imagine something like if the app is killed and those methods aren't called, the process itself is killed so any resource pointers you have might be garbage collected or otherwise cleaned up for you. In that case, onStop()/onDestroy() might be responsible for cleaning up resources that would be held onto until the process is killed. That seems a bit convoluted, however.

This is basically correct for onDestroy(). The point is that you should free up resources that it would be wasteful to keep if your process is going to survive, but without that Activity continuing to be needed. Think of this as the case where only the individual Activity is being destroyed.

In contrast, if the process hosting your Activity is being killed, it is the operating system which will do the cleanup of resources held by the process. Think of this as the case where everything is being destroyed (though that is not strictly accurate, as you can have multiple processes).

There have historically been some issues with things like in-use status of the camera that did not get properly cleaned up by the operating system, but the current status of that would be a distinct issue.

The purpose of onStop() however is somewhat different - while it can happen on the way to death, this does not itself indicate the death of an Activity, but rather only that the Activity is ceasing to be visible on screen. Hence this could be a time to stop visual updates and potentially release resources involved with that, as well as other behavior that you want to have linked to on-screen status.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM