是否必须从Android生命周期中删除自己作为观察者?

我正在构建一个实现LifecycleObserver接口的Android java类.

这是构造函数

public MyObserver(AppCompatActivity activity) {
    this.mActivity = new WeakReference

是否有必要使用以下内容调用removeObserver

@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
public void destroyListener() {
    if (this.mActivity.get() != null) {
        this.mActivity.get().getLifecycle().removeObserver(this);
    }
}

或者,我可以永远观察吗?

最佳答案
TL; DR:不.

根据这个link here,用户在android-lifecycles Github repo上询问了你的问题. Google开发人员对此问题的回答是:

Yes,that’s the whole point of the new lifecycle-aware components,no
need to unsubscribe/remove observers.

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...