How to use setArguments() and getArguments() methods in Fragments?

Android_programmer_camera picture Android_programmer_camera · Mar 24, 2011 · Viewed 157.1k times · Source

I have 2 fragments: (1)Frag1 (2)Frag2.

Frag1

bundl = new Bundle();
bundl.putStringArrayList("elist", eList);

Frag2 dv = new Frag2();
dv.setArguments(bundl);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.the_fragg,dv);
ft.show(getFragmentManager().findFragmentById(R.id.the_fragg)); 
ft.addToBackStack(null);
ft.commit();

How do I get this data in Frag2?

Answer

ashakirov picture ashakirov · Aug 21, 2012

Just call getArguments() in your Frag2's onCreateView() method:

public class Frag2 extends Fragment {

     public View onCreateView(LayoutInflater inflater,
         ViewGroup containerObject,
         Bundle savedInstanceState){
         //here is your arguments
         Bundle bundle=getArguments(); 

        //here is your list array 
        String[] myStrings=bundle.getStringArray("elist");   
     }
}