2

I am using Android Navigation Component to navigate between fragment screens.

After navigating to a new fragment screen, I want to unregister all the LiveData observations that I registered using the fragment life cycle. The problem is that the fragment is not getting detached until the navigation is complete and I am getting unintended events from LiveData.

How to handle this?

I tried using app:popUpTo but that happens too late.

Any ideas on how to solve this?

Ilya Gazman
  • 31,250
  • 24
  • 137
  • 216
  • 1
    What does "until the navigation is complete" mean? Your Fragment is stopped immediately (the next frame) after you call `navigate()`. LiveData does not send events to stopped fragments. – ianhanniballake Jun 24 '21 at 22:12
  • Can you upload a sample code in which you call `observe`? The beauty in `LiveData` is that you don’t need to worry about observations since it’s automatically bound the the lifecycle of the fragment. I want to see which lifecycle owner you send to `observe` function – Shlomi Katriel Jun 25 '21 at 14:03
  • @ShlomiKatriel I use `this` that resolves to Fragment. – Ilya Gazman Jun 25 '21 at 14:11
  • I believe you need to use `viewLifecycleOwner` (or something similar, I'm writing from memory) instead `this`. Please try and update. – Shlomi Katriel Jun 26 '21 at 04:31

0 Answers0