我通过 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/