Why override Activity.onDestroy() if it isn't reliably called?

前提是你 提交于 2019-12-05 21:30:46

Why override Activity.onDestroy() if it isn't reliably called?

It's not that it isn't reliably called... it's just that it isn't the only way the Activity can be killed. The Android system might trash your entire process without giving the ActivityManager the chance to call onDestroy() if your device begins to lack memory resources.

For this reason, you shouldn't ever rely on onDestroy() being called, and you should always save persistent state in onPause.

nandeesh

Objects held by the activity will get destroyed if the process is killed directly. If the process is not killed (and onDestroy() is called) then you will have to manually release the objects if needed. E.g, when the process is killed, a Cursor will be destroyed, but if the process is not destroyed and you repeatedly enter the activity there will be resource leakage.

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