Issue
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?
Solution
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).
Answered By - sergpetrov
Answer Checked By - Clifford M. (JavaFixing Volunteer)