在 LifecycleObserver 中持有 Lifecycle 对象的引用

Holding reference of Lifecycle object in LifecycleObserver

在 LifecycleObserver 中 class 持有 Lifecycle 对象的引用有多好?会出现什么并发症?

class MyLocationListener implements LifecycleObserver {
    private Lifecycle mLifecycle;
    public MyLocationListener(Context context, Lifecycle lifecycle, Callback callback) {
           mLifecycle = lifecycle  
           ...
    }
}

你不应该保留一个引用,它是内存泄漏的一个开口,Observer 需要观察不包含引用,如果你想对生命周期更改执行操作,请这样做:

 @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY //you can change to whatever lifecycle event you need)
 public void activityDestroied() {
 //actions here
 }