java - Lifecycle.removeObserver 应该在 Activity 中调用吗? - 安卓

标签 java android kotlin

我通过 LifecycleObserver 找到了明确的答案。

class MyActivity : AppCompatActivity(), LifecycleObserver {
    override fun onCreate() {
       ...
       this.lifecycle.addObserver(this)
    }
}

在这种情况下,我应该在 onDestroy() 处调用removeObserver(this) 吗? 我认为没有必要,但我无法确定。

我想要一份官方文档或真正的库代码,而不仅仅是意见。

最佳答案

不,您不需要调用removeObserver()

在文档中找到有关此内容的任何内容都非常困难。有一个Issue about this在生命周期代码实验室的存储库中,有人问:

Here, there is addObserver() call, but there is no removeObserver() call for onDestroy. Is this intentional?

Jose Alcerreca(Google 的 Android 开发者关系工程师)说道:

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

关于java - Lifecycle.removeObserver 应该在 Activity 中调用吗? - 安卓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67704532/

相关文章:

java - Android:如何以编程方式启用/禁用 Wifi 或 Internet 连接

android - 为什么即使初始化了Lateinit属性,我仍会得到kotlin.UninitializedPropertyAccessException(可能)

android - Kotlin 代码堆栈跟踪显示 Java 行号

java - 从 RecyclerView 中删除项目会导致项目重复

在 Android 上运行 libgdx 应用程序时出现 java.lang.NoClassDefFoundError

android - React-Native 无法获取/和 JS 包

Android:如何在图表引擎下创建列表?

在命令行中执行但不在浏览器中执行的 Java 文件

java - JTabbedPane 滚动按钮位置

android - Foursquare用户 checkin V2