Why implement onDestroy() if it is not guaranteed to be called?

吃可爱长大的小学妹 提交于 2019-11-27 11:53:34

onDestroy will be called if you explicitly call finish(); yourself.

Your main activity calls startActivityForResult on a map activity.

Map activity with a LocationListener, the user clicks the map and selects say a local restaurant.

The activity then , sets up some extras to be sent back to your main activity, it then explicitly call's finish(); on itself and in the onDestroy kills the LocationListener and other variables you had invoked.

Just found this in the docs

onDestroy() = The final call you receive before your 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.

Can you describe a real-app situation (i.e. not analogy to driving a car etc.) in which it would make sense to implement onDestroy()?

When you want to capture a configuration change. It's all in the SDK: http://developer.android.com/reference/android/app/Activity.html

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