gpt4 book ai didi

android - Observer 和 LifecycleObserver 有什么区别?

转载 作者:行者123 更新时间:2023-11-29 02:29:03 25 4
gpt4 key购买 nike

我很难理解 LiveDataLifecycleObserverObserver 之间的区别 LiveData 对象在 observe() 方法中接收。

LiveData 是一个 LifecycleObserver 并且因此它知道 Activity 是否正确?

并且 observe() 方法中接收到的 ObserverLifecycleObserver 无关,因为它只是用来执行 onChange() 方法,当 LiveData 发生变化时?

最佳答案

Am I right saying that the LiveData is a LifecycleObserver and so it knows about the Activity because of it?

是的。

And that the Observer received in the observe() method has nothing to do with LifecycleObserver because it is there just to execute the onChange() method when there is a change in LiveData?

是的。


顾名思义,LifecycleObserver 应该跟踪其父级(ActivityFragment 或任何其他 LifecycleOwner),使 LiveData 生命周期感知。

另一方面,Observer 跟踪此 LiveData 对象的 value 发生的变化。

所以你是对的。 ObserverLifecycleObserver 无关。

关于android - Observer 和 LifecycleObserver 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50660304/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com