0

I am using nested fragments so fragment1 opens fragment2 and they share a viewmodel. This viewmodel calls a repository to get data and using a LiveData it posts the result and then fragment2 displays the data.

My problem was the second time fragment1 opened fragment2, for a brief moment fragment2 was displaying the previous data. I could solve this problem using a similar approach to this post Android Arch Components ViewModel and LiveData trigger after screen rotation using SingleLiveEvent.

However, now my issue is that on screen rotation, data is not delivered again (since it is done a single time) and fragment2 screen remains blank.

I was thinking on modifying SingleLiveEvent so it sometimes delivers data a single time but other times to make it deliver again using a condition or something similar.

Does anybody knows what approach could I use here?

  • Have you considered using a custom `LifeCycleObserver` class and attaching it to the `Fragment`. You can find how to do it [here](https://stackoverflow.com/a/58511176/9722336) – gtxtreme Aug 23 '21 at 16:47
  • Also you can stick to the `LiveData` class and don't need to use special `LiveData` subclasses. This would be beautifully solved by checking the concept of Lifecycles especially in the case of Fragments. I'd recommend that you read about it – gtxtreme Aug 23 '21 at 16:49

0 Answers0