Reputation:
I have an Activity that makes extensive use of Fragments.
The usual flow is:
Fragment A -> Fragment B -> Fragment C -> Fragment D
And sometimes I go back directly from D to B with
int id = getFragmentManager().getBackStackEntryAt(1).getId();
getFragmentManager().popBackStack(id, 0);
It works fine. However, I don't have any reference in my Activity to Fragment B.
What's the best way to send Fragment B data that I just got in Fragment D?.
Upvotes: 2
Views: 3185
Reputation: 3476
Fragments are meant to be reusable, isolated UI components. They generally shouldn't have knowledge of each other. In keeping with the design principle of separation of concerns you should have some sort of underlying data object that the fragments can operate on to save data.
Either the parent Activity can pass a reference to this data structure, or it can be a globally accessible object somehow (this question is abstract enough that providing a concrete response isn't feasible).
Upvotes: 8