0

Since Fragments were introduced I keep my eyes open for a good solution to update a Fragment to which I'm going back to.

This is my scenario:

I have a One-Activity App. When the App starts it shows a FirstFragment. With a button click the user can open a SecondFragment. Both Fragments are full screen and will be opened via androids NavController.

Note: Both fragments get their data from a rx-based repository. The repository and its underlying classes handle all CRUD operations.

What I want to do:

Updating the FirstFragment as soon as the user clicks back on the SecondFragment OR in other words: I want to update the FirstFragment as soon as it comes on top from the background.

The Problem

The FirstFragment gets no callback or livecycle event if it comes into view again.

In the past I solved this problem with a ListenerInterface that was triggered in the activities onBackPressed or with a broadcast event. But is there no better solution? I question if there is a possibility via LiveData or via NavController or anything else. Or does anybody know how google solves this problem?

What I NOT want to do

As my data layer is based on rx I could of course have a BehaviourSubject that I subscribe on in the FirstFragment. Then when I switch to SecondFragment I could keep the subscription undisposed so the FirstFragment will be updated as soon as i call onNext on the BehaviourSubject. I've seen apps doing this and it seems wrong to me. In my opinion a subscription should be disposed as soon as another ViewModel steps into place.

Thanks in advance

Android, notify backstack fragments on some change

Emanuel
  • 863
  • 9
  • 29
  • 1
    How about sharing the viewmodel between the fragments if there is enough common logic involved? – denvercoder9 Dec 05 '19 at 09:50
  • I agree with @sonnet. sharing viewmodel and observing livedata would fit your case. – Choim Dec 05 '19 at 09:57
  • other way to update the fragment when it appears is use override method setUserVisibleHint(boolean isVisibleToUser), it returns boolean when fragment visibility changes. – Raza Dec 05 '19 at 10:03
  • @Raza `setUserVisibleHint` is deprecated. – EpicPandaForce Dec 05 '19 at 10:10
  • @Emanuel `FirstFragment` should be getting `onStart` event. Using `LiveData` should automatically trigger a resubscription in `onStart`. – EpicPandaForce Dec 05 '19 at 10:10
  • @EpicPandaForce The FirstFragment does not get any lifecycle event at all. If it would get an onStart event it would be sufficient for me. – Emanuel Dec 05 '19 at 14:56
  • @sonnet to share the viewmodel is an interesting idea but not suitable for me. I have a huge app and the navigation is not as straight as i.e. settings. I can navigate to a screen from different fragments and need this update mechanism nearly everywhere. That would mean I have a lot of viewmodels to share which would make my viewmodel class too heavy. – Emanuel Dec 05 '19 at 14:59
  • Then the only option I see is, extract that shared code in a separate class (usecase/interactor/whatever), expose an rx observable, make it scoped (application /activity/logged-in-user/singleton scope), then inject it into the viewmodels through dagger. – denvercoder9 Dec 05 '19 at 16:12
  • There is an OnDestinationChangedListener that could be worth checking out. I will update my post as soon as I found time to investigate this option. https://developer.android.com/guide/navigation/navigation-ui – Emanuel Dec 10 '19 at 14:35
  • The OnBackPressedCallback could be a help. I will check this out https://developer.android.com/guide/navigation/navigation-custom-back – Emanuel Dec 10 '19 at 14:39
  • Here is another thread about a similar topic https://stackoverflow.com/questions/11326155/fragment-onresume-onpause-is-not-called-on-backstack – Emanuel Dec 10 '19 at 14:45

0 Answers0