Judy T Raj Judy T Raj - 20 days ago 8
Android Question

How can I pass data between two fragments belonging to same container activity?

I've an arraylist in one fragment that I need to pass to another fragment. Both of these fragments belong to the same container activity.
This is what I came up with but apparently there's something wrong with it.

This is how I'm sending the data from Fragment1:

Bundle b=new Bundle();
b.putStringArrayList("Brands",allBrands);
Fragment fragment = new Fragment();
fragment.setArguments(b);


This is how I'm trying to receive the data at Fragment2:

brands = getArguments().getStringArrayList("Brands");


I understand this is not how it is done. Please help me out.
I checked the question on implementing fragmentlistener.This isn't about that.

r47 r47
Answer

There are few different ways of communicating between fragments.

1) creating a interface 2) shared preferences 3) sqlite database

using 1) you create a common method where the information can be read by both fragments.

using 2) is great to save the information forever until overwritten or deleting the app.

using 3) same as the second one but it depends on the app if you really need a database or not.