44

As of 2019, I'm trying to follow a best practice on where to start observing LiveData in Fragments and if I should pass this or viewLifecycleOwner as a parameter to the observe() method.

  • According to this Google official documentation, I should observe in onActivityCreated() passing this (the fragment) as parameter.

  • According to this Google sample, I should observe in onViewCreated() passing viewLifecycleOwner as parameter.

  • According to this I/O video, I shouldn't use this but instead viewLifecycleOwner, but doesn't specify where should I start observing.

  • According to this pitfalls post, I should observe in onActivityCreated() and use viewLifecycleOwner.

So, where should I start observing? And should I either use this or viewLifecycleOwner?

fernandospr
  • 2,976
  • 2
  • 22
  • 43
  • 7
    I feel your pain. Android is moving really fast with Jetpack and best practices are all over the place. – maxbeaudoin Aug 12 '19 at 19:17
  • 1
    Adding a new source where it states it should be done in `onCreateView()` using `viewLifecycleOwner` https://codelabs.developers.google.com/codelabs/kotlin-android-training-live-data/#4 – fernandospr Apr 23 '20 at 14:05

3 Answers3

25

If observing from an Activity you can observe on onCreate() and use this for the LifecycleOwner as stated here:

If you have a lifecycle-aware component that is hooked up to the lifecycle of your activity it will receive the ON_CREATE event. The method annotated with @OnLifecycleEvent will be called so your lifecycle-aware component can perform any setup code it needs for the created state.

Now if you are observing within a Fragment you can observe on onViewCreated() or onActivityCreated() and you should use getViewLifecycleOwner() and here is why:

Get a LifecycleOwner that represents the Fragment's View lifecycle. In most cases, this mirrors the lifecycle of the Fragment itself, but in cases of detached Fragments, the lifecycle of the Fragment can be considerably longer than the lifecycle of the View itself.

Rodrigo Queiroz
  • 2,674
  • 24
  • 30
  • 3
    ```onActivityCreated``` is deprecated only option is ```onViewCreated``` – Alok Vishwakarma Oct 28 '21 at 15:43
  • 1
    DO NOT PASS `viewLifecycleOwner` for `DialogFragment`. When subscribing to lifecycle-aware components such as `LiveData`, you should never use `viewLifecycleOwner` as the `LifecycleOwner` in a `DialogFragment` that uses `Dialogs`. Instead, use the `DialogFragment` itself, or if you're using Jetpack Navigation, use the `NavBackStackEntry`. ref:https://developer.android.com/guide/fragments/dialogs – iCantC Mar 14 '22 at 08:44
2

As in the I/O talk Yigit says, the Fragment and its view has different lifecycles. You would need to identify if your LiveData is related to the fragment or its view and pass the one desired. The compiler will accept both since both are implementations of LifecycleOwner

Arka Prava Basu
  • 2,366
  • 3
  • 18
  • 34
-1

It doesn't matter whether you do it on onViewCreated or onActivityCreated. Both are called when the fragment is inflated, onViewCreated first, onActivityCreated afterwards. It's really a matter of preference.

The LiveData object takes a LifecycleOwner, and both Fragment and Activity implement the interface, so you just need to pass this.

Francesc
  • 25,014
  • 10
  • 66
  • 84
  • So, `viewLifeCycleOwner` is not a part of the equation? – Taseer Jul 17 '19 at 18:15
  • 1
    `viewLifeCycleOwner` refers to the lifecycle of the root view in the fragment. For most intents and purposes, that lifecycle is the same as the fragment, but in some edge cases they might be different. Unless you have a specific need to track that lifecycle, I'd suggest to stick to `this`. – Francesc Jul 17 '19 at 18:27
  • ```onActivityCreated``` is deprecated only option is ```onViewCreated``` – Alok Vishwakarma Oct 28 '21 at 15:42