What is the difference between Observer and LifecycleObserver?

659 views Asked by At

I am struggling to understand the difference between LiveData being a LifecycleObserver and the Observer that the LiveData object receives in the observe() method.

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?

1

There are 1 answers

0
Rosário Pereira Fernandes On

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

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?

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.

The Observer on the other hand keeps track of changes happening to the value of this LiveData object.

So you are correct. Observer has nothing to do with LifecycleObserver.