0

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.

Judy T Raj
  • 1,755
  • 3
  • 27
  • 41
  • Possible duplicate of [How to implement OnFragmentInteractionListener](http://stackoverflow.com/questions/24777985/how-to-implement-onfragmentinteractionlistener) – OneCricketeer Nov 16 '16 at 14:46
  • Go through the [link](http://stackoverflow.com/questions/33158378/passing-arreylist-between-fragments-with-parcelable) – Suresh A Nov 16 '16 at 14:55
  • 1
    The [official documentation](https://developer.android.com/training/basics/fragments/communicating.html) says: _"All Fragment-to-Fragment communication is done through the associated Activity. Two Fragments should never communicate directly"_ and explains the idea in more detail. – Markus Kauppinen Nov 16 '16 at 14:57

1 Answers1

3

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.

r47
  • 46
  • 4