I want callback in fragment when it's not currently visible
Ex: I am opening FragmentA
then I am opening FragmentB
now I am updating list sequence in FragmentB
after updating list sequence I also want to update list in FragmentA
before opening it (want to update list in FragmentA
when FragmentB
is visible)
I have not perform .popBackStack()
in FragmentB
Now if I update list seqeuence 2nd time then list sequence in FragmentA
should update
In sort : I want callback in fragment when it's not currently visible
Is it posible?
CodePudding user response:
your best option is using the shared view model in which your both fragment will use on view Model and you don't need to worry about the lifecycle of your fragments. take a look at the link below
CodePudding user response:
You have several options to achieve this, it depends on many factors such as the context of your task, what type of data you want to share between fragments, what type of architecture in your project, and whether the data should be persistent in the memory or, database or another cache, etc.
- If you want to pass a one-time simple data to Fragment A then you can use Fragment Result API
- If you are using Android Architecture Components and MVVM then try Shared View Model or NavGraph View Model
- If you have a DI you can use some scoped instance to temporarily keep and share your data (for example a Repository), so the scope of life of the instance should be longer than these 2 fragments.
- If you have storage that allows changes observing then you can subscribe to the changes in Fragment A (for example Room Observing database changes).