Adam Hurwitz
1 min readAug 17, 2018

--

Great job diving into this complex topic Christophe.

I have multiple LiveData Observers that are being triggered in a Fragment after navigating to a new Fragment, popping the new Fragment, and returning to the original Fragment.

Details: The architecture consists of MainActivity that hosts a HomeFragment as the start destination in the MainActivity’s navigation graph. Within HomeFragment is a programmatically inflated PriceGraphFragment. The HomeFragment is using the navigation component to launch a new child Fragment ProfileFragment. On back press the ProfileFragment is popped and the app returns to the HomeFragment hosting the PriceGraphFragment. The PriceGraphFragment is where the Observer is being called multiple times.

You can see the StackOverflow more detailed implementation.

Attempted Solutions

  1. Creating the Fragment’s ViewModel in the onCreate() method.
    priceViewModel = ViewModelProviders.of(this).get(PriceDataViewModel::class.java)
  2. Moving methods that create the Observers to the Fragment’s onCreate() method.
  3. Using viewLifecycleOwner instead of this for the LifecycleOwner in the method observe(@NonNull LifecycleOwner owner, @NonNull Observer<? super T> observer).

--

--

Adam Hurwitz
Adam Hurwitz

Written by Adam Hurwitz

An account about nothing | Researcher and product consultant

No responses yet