简体   繁体   English

观察者和生命周期观察者有什么区别?

[英]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作为LifecycleObserverLiveData对象在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()方法中接收到的ObserverLifecycleObserver无关,因为它只是在LiveData发生变化时执行onChange()方法?

Am I right saying that the LiveData is a LifecycleObserver and so it knows about the Activity because of it?我说LiveData是一个LifecycleObserver并且因此它知道Activity是否正确?

Yes.是的。

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()方法中接收到的ObserverLifecycleObserver无关,因为它只是在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应该跟踪其父级( ActivityFragment或任何其他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 . ObserverLifecycleObserver无关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM