1

If we scope LiveData to the lifecycle of a fragment by passing this to the observe method, the fragment doesn't get immediate updates if it is only detached from the activity, but not removed, because only the view is destroyed, not the fragment instance itself.

Instead, we can scope LiveData to the lifecycle of the view of the fragment, by calling observe in onActivityCreated and passing getViewLifecycleOwner() rather than this.

Is there any reason to not scope the LiveData to the fragment's view?

Florian Walther
  • 6,237
  • 5
  • 46
  • 104

1 Answers1

1

If your fragment does not have a UI then you will need to scope it with fragment's lifecycle. Many people/libraries used and still use headless(UI-Less) worker fragments to have lifecycle awareness to safely pass on asynchronously retrieved data (Network Call) to the UI. (Similar to what Loaders do)

Vishal Arora
  • 2,524
  • 2
  • 11
  • 14