为什么要实施的onDestroy(),如果它不能保证被称为? [英] Why implement onDestroy() if it is not guaranteed to be called?

查看:170
本文介绍了为什么要实施的onDestroy(),如果它不能保证被称为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据android的活动生命周期,保证被调用(如果活动一旦脱离运行状态,这是典型的预期)唯一的回调的onPause()

According to the android Activity Lifecycle, the only callback guaranteed to be called (if an activity ever leaves the Running state, which is typically expected) is onPause().

所以,我必须假定有场景中,是有意义的实施的onStop()的onDestroy()虽然他们不是真正的保证的调用。

So, I must assume that there are scenarios in which it makes sense to implement onStop() and onDestroy() although they are not really guaranteed to be called.

据我所知,的onStop()时,有可能为活动通过停止状态恢复到运行状态(为什么会那样做,而不是返回应实施直接是一个不同的问题)。

I understand that onStop() should be implemented when it's possible for an activity to return to the Running state via the Stopped state (why would it do that instead of returning directly is a different question).

但是需要的onDestroy(),当我可以将所有的清理/状态保存到的onPause(),我不清楚。

But the need for onDestroy(), when I can place all cleanup/state-saving into onPause(), is unclear to me.

你能描述一个真实的应用程序的情况(即不是比喻驾驶汽车等),这是明智实施的onDestroy()

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()?

推荐答案

的onDestroy将被调用,如果你显式调用完成();你自己。

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

您的主要活动呼吁startActivityForResult地图上的活动。

Your main activity calls startActivityForResult on a map activity.

用LocationListener的地图活动,用户点击地图,选择说当地餐厅。

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

活动的话,设置发送回您的主要活动一些额外的,它会显式调用的完成();对自己和杀死的onDestroy的LocationListener的和其他变量,你已经执行。

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.

刚刚发现这个在文档

的onDestroy()=你的活动前收到的最后通话将被破坏。发生这种情况可能是因为该活动结束(有人称为完成()就可以了,或因为系统暂时销毁活动的这种情况下,以节省空间,你可以用isFinishing()方法,这两种方案之间的区别。

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.

这篇关于为什么要实施的onDestroy(),如果它不能保证被称为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆