我试图了解在onDestroy取消注册监听器之前是否有任何潜在的危害.我特别指的是从事件总线(otto)取消订阅,但相信答案适用于大多数侦听器模式实现(SharedPreferenceListener,LocationListener,静态引用等).
我已经看到several other answers这个引用短语’onDestroy不能保证被调用’的问题.完整的onDestroy documentation文本实际上是:
Note: do not count on this method being called as a place for saving data! For example, if an activity is editing data in a content provider, those edits should be committed in either onPause() or onSaveInstanceState(Bundle), not here. This method is usually implemented to free resources like threads that are associated with an activity, so that a destroyed activity does not leave such things around while the rest of its application is still running. There are situations where the system will simply kill the activity’s hosting process without calling this method (or any others) in it, so it should not be used to do things that are intended to remain around after the process goes away.
我理解这意味着onDestroy不应该用于保存数据,因为它可能在应用程序进程被杀死之前不被调用(如生命周期图所示).
简单地说,由于没有调用onDestroy,是否存在注册为侦听器的Activity可能被泄露的情况?