Home > Mobile >  What should be lifeCycleOwner of LiveData-Fragment or Activity?
What should be lifeCycleOwner of LiveData-Fragment or Activity?

Time:09-26

Will Activity be lifeCycleowner of LiveData?

or

Will Fragment be lifeCycleowner of LiveData?

CodePudding user response:

As stated in the Google documentation

Each Fragment instance has its own lifecycle. When a user navigates and interacts with your app, your fragments transition through various states in their lifecycle as they are added, removed, and enter or exit the screen.

So each fragment and activity have their own lifecycle.

When you observer LiveData in the fragment or activity you pass the lifecycleowner associated with that fragment or activity.

CodePudding user response:

The answer is as per my understanding and I would say, it depends on where LiveData is being used or called from?

Let's go through some basics.

LifecycleOwner by definition means

A class that has an Android lifecycle. These events can be used by custom components to handle lifecycle changes without implementing any code inside the Activity or the Fragment.

Lifecycle

Lifecycle-aware components perform actions in response to a change in the lifecycle status of another component, such as activities and fragments. These components help you produce better-organized, and often lighter-weight code, that is easier to maintain.

Since the Lifecycle of Activity and Fragments are different.

  1. Activity - Activity has its own lifecycle
  2. Fragment - Each Fragment instance has its own lifecycle. To manage the lifecycle, Fragment implements LifecycleOwner.

Fragments are basically contained inside an Activity; so if the Activity is destroyed, Fragments will also be destroyed.

But it's not necessary that if fragments are destroyed, Activity will also be destroyed.

Live Data

You can register an observer paired with an object that implements the LifecycleOwner interface. This relationship allows the observer to be removed when the state of the corresponding Lifecycle object changes to DESTROYED.

Now coming back to your question.

  • when LiveData is observed in Activity, the lifecycle owner is Activity itself.

  • when LiveData is observed in Fragment, the lifecycle owner is Fragment itself.

CodePudding user response:

What should be lifeCycleOwner of LiveData-Fragment or Activity?

It really depends on where you're using liveData. LiveData is lifecycle-aware, which means they respect the lifecycle of android app components.

If you are using liveData to observe data inside activity, then you should observe using activity.

If you are using liveData to observe data inside a fragment, then there is no point in using activity as lifecycleOwner. Using activity would lead to a memory leak, if fragment is replaced or removed, as liveData would still hold a strong reference to the fragment, which will be kept until activity is destroyed.

We should use viewLifecycleOwner inside fragments, as they are bind to the lifecycle of fragments.

  • Related