在LifecycleObserver中保留Lifecycle对象的引用

时间:2017-06-17 18:03:30

标签: android android-architecture-components android-architecture-lifecycle

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

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

1 个答案:

答案 0 :(得分:-1)

你不应该保留一个引用,它是内存泄漏的开放,Observer需要注意不要包含引用,如果你想对生命周期更改执行操作,请执行以下操作:

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