[英]What is the difference between Observer and LifecycleObserver?
I am struggling to understand the difference between LiveData
being a LifecycleObserver
and the Observer
that the LiveData
object receives in the observe()
method.我正在努力理解
LiveData
作为LifecycleObserver
和LiveData
对象在observe()
方法中接收到的Observer
之间的区别。
Am I right saying that the LiveData
is a LifecycleObserver
and so it knows about the Activity
because of it?我说
LiveData
是一个LifecycleObserver
并且因此它知道Activity
是否正确?
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
?并且在
observe()
方法中接收到的Observer
与LifecycleObserver
无关,因为它只是在LiveData
发生变化时执行onChange()
方法?
Am I right saying that the
LiveData
is aLifecycleObserver
and so it knows about theActivity
because of it?我说
LiveData
是一个LifecycleObserver
并且因此它知道Activity
是否正确?
Yes.是的。
And that the
Observer
received in theobserve()
method has nothing to do withLifecycleObserver
because it is there just to execute theonChange()
method when there is a change inLiveData
?并且在
observe()
方法中接收到的Observer
与LifecycleObserver
无关,因为它只是在LiveData
发生变化时执行onChange()
方法?
Yes.是的。
As the name suggests, a LifecycleObserver
is supposed to keep track of changes happening to the lifecycle of its parent ( Activity
or Fragment
or any other LifecycleOwner
), making LiveData
lifecycle-aware.顾名思义,
LifecycleObserver
应该跟踪其父级( Activity
或Fragment
或任何其他LifecycleOwner
)生命周期中发生的更改,从而使LiveData
生命周期感知能力。
The Observer
on the other hand keeps track of changes happening to the value
of this LiveData
object.另一方面,
Observer
会跟踪此LiveData
对象的value
发生的变化。
So you are correct.所以你是对的。
Observer
has nothing to do with LifecycleObserver
. Observer
与LifecycleObserver
无关。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.