lazyguy
lazyguy

Reputation: 963

Data sharing between fragments

Here is my situation. I have a List<Data> and MainActivity and FragmentViewA, FragmentViewB, FragmentViewC. These fragments share the same data but show it differently.

If one fragment change the List<Data> then other fragments needs to be updated.

Since loading of this List is expensive can I store this in MainActivity as a member variable? Then on OnSaveInstance() we will save the List<Data>to the bundle and retrieve in onCreate() This way when the activity is reclaimed we don't loose data.

Now MainActivity can implement listeners that will listen for any change in the List<Data> and if a change occur then notify other fragments.

Upvotes: 1

Views: 40

Answers (1)

ligi
ligi

Reputation: 39529

you could use a messagebus like otto (https://github.com/square/otto)

Upvotes: 1

Related Questions