I am finding clear answer with LifecycleObserver.
class MyActivity : AppCompatActivity(), LifecycleObserver {
override fun onCreate() {
...
this.lifecycle.addObserver(this)
}
}
In this situation, should I call removeObserver(this) at onDestroy()? I think it is not necessary, but I couldn't sure it.
I want an official document or real library code, not just opinion.
No you don't need to call removeObserver()
.
It's surprisingly difficult to find anything about this in the docs. There is an Issue about this in the repo for the code lab for Lifecycles, where someone asks:
Here, there is addObserver() call, but there is no removeObserver() call for onDestroy. Is this intentional?
And Jose Alcerreca (an Android Developer Relations Engineer at Google) says:
Yes, that's the whole point of the new lifecycle-aware components, no need to unsubscribe/remove observers.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.