1

在 LifecycleObserver 类中,保存 Lifecycle 对象的引用有多好?会出现什么并发症?

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

1 回答 1

-1

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

 @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY //you can change to whatever lifecycle event you need)
 public void activityDestroied() {
 //actions here
 }
于 2017-06-17T18:11:36.910 回答