0

My Singleton Class: Here's the Singleton class I found on Official Documentations.

    public class Singleton {
    private static Singleton uniqInstance;
    private Singleton() {
    }
    public static Singleton getInstance() {
        if (uniqInstance == null) {
            {
                if (uniqInstance == null)
                   uniqInstance = new Singleton();
            }
        }
        return uniqInstance;
    }
   } 

My First Fragment: This is my first fragment, Here I'm Adding ArrayList items with object class Names.

     public View onCreateView(LayoutInflater inflater, ViewGroup 
          container, Bundle savedInstanceState){
          View mView = inflater.inflate(R.layout.fragment_first, container,false);
          RecyclerView rvFirst = mView.findViewById(R.id.rv_first_layout);
          rvFirst.setHasFixedSize(true);
          rvFirst.setLayoutManager(new LinearLayoutManager(mView.getContext()));
          mArrayList = new ArrayList<Names>();
          mArrayList.add(new Names("Hello"));
          mArrayList.add(new Names("This"));
          mArrayList.add(new Names("Is"));
          mArrayList.add(new Names("Android"));

          // HERE I WANT TO SEND ARRAY-LIST DATA TO THE SECOND FRAGMENT
          mAdapter = new NameAdapter(mArrayList);
          rvFirst.setAdapter(mAdapter);
          return mView;
    }        

My Second Fragment: This is my second fragment, Here I'm trying to get ArrayList items from First Fragment.

     public View onCreateView(LayoutInflater inflater, ViewGroup 
          container,Bundle savedInstanceState){
          View mView = inflater.inflate(R.layout.fragment_second, container,false);
          rvSecond = (RecyclerView) mView.findViewById(R.id.rv_second_layout);
          rvSecond.setHasFixedSize(true);        
          rvSecond.setLayoutManager(new LinearLayoutManager(getContext()));        
          mArrayList = new ArrayList<Names>(); 

          // HERE I WANT TO GET ARRAY-LIST FROM FIRST FRAGMENT     
          mAdapter = new NameAdapter(mArrayList);
          rvSecond.setAdapter(mAdapter);
     return mView;
   }
Hemant Parmar
  • 3,924
  • 7
  • 25
  • 49
Jay Halani
  • 913
  • 1
  • 9
  • 12

4 Answers4

2

why using singleton for this, if your both fragments are attached to the same activity then you can do this,

first create a public list variable in your acitivity class

then reference it from your fragment classes by its context like

((YourActivityClass)getActivity()).myArrayList.get(i);
  • 1
    #Abhinay Sharma...what if i need the same data in the fragment not attached with the same activity,that's the reason singleton class is necessarily to get data anywhere in your application. – ashish Jan 14 '19 at 06:10
2

First set the arraylist inside the singleton class like this

Your singleton class should be like this:-

public class Singleton {
private static Singleton uniqInstance;
public ArrayList<Names> names = new ArrayList<Names>();;
private Singleton() {
}
public static Singleton getInstance() {
    if (uniqInstance == null) 
         uniqInstance = new Singleton();
    return uniqInstance;
}
public void setArrayList(ArrayList<Names> names)
 {
      this.names = names;

     }
 public ArrayList<Names> getArrayList()
 {
      return this.names;

     }
} 

In your first fragment after adding values in list:-

Singleton.getInstance().setArrayList(mArrayList);

In your second fragment get it like this :-

Singleton.getInstance().getArrayList();
Deeksha
  • 536
  • 5
  • 14
Santanu Sur
  • 10,997
  • 7
  • 33
  • 52
0

You can pass it through the hosting Activity or store it in the extended Application class. Official documentation.

Other options include EventBus, Intent, MessageLooper.

The EventBus is dead simple but if you overuse it the code will become spaghetti.

loshkin
  • 1,600
  • 2
  • 21
  • 38
0

To pass data between fragment follow these steps :

Bundle bundle = new Bundle();
putParcelableArrayList("ArrayListKey", mArrayList);

Fragment fragment = new MasterSearchTabFragment();
fragment.setArguments(bundle);

//Add Fragment while starting it   
getFragmentManager()
                .beginTransaction()
                .add(R.id.frame_layout_root_container, fragment)
                .commit();

In Second Fragment :

 Bundle mBundle = new Bundle();
 mBundle = getArguments();

if (null != mBundle) {
    ArrayList arrayList =  getParcelableArrayList("ArrayListKey");
}
Jeelan
  • 173
  • 1
  • 11