103

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?

Srikar Reddy
  • 3,470
  • 4
  • 34
  • 54
Android_programmer_camera
  • 12,579
  • 20
  • 65
  • 81

7 Answers7

193

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");   
     }
}
Vasily Kabunov
  • 5,655
  • 12
  • 47
  • 49
ashakirov
  • 11,513
  • 5
  • 39
  • 40
  • 12
    It is returning null in my case, any idea why this is happening ? – Anirudh Apr 01 '13 at 10:09
  • 2
    You're putting **ArrayList** into the bundle, but getting a String Array. You should do `bundle.getStringArrayList("elist");` – Rafał Oct 14 '14 at 10:18
  • 1
    You forgot the return statement: `return super.onCreateView(inflater, container, savedInstanceState);` – user41805 Jan 01 '16 at 18:29
  • 4
    Oncreateview calling all the time. So simply call getarguments in oncreate() method. It will call only when the fragment is destroyed or newly created time. – Mohamed Ibrahim Jan 29 '16 at 03:54
  • 5
    @almaz_from_kazan @HabeebPerwad Why are you using `getArguments()` in `onCreateView`, not in `onCreate`? – Nik Kober May 23 '16 at 10:35
43

Eg: Add data:-

   Bundle bundle = new Bundle();
   bundle.putString("latitude", latitude);
   bundle.putString("longitude", longitude);
   bundle.putString("board_id", board_id);
   MapFragment mapFragment = new MapFragment();
   mapFragment.setArguments(bundle);

Eg: Get data :-

String latitude =  getArguments().getString("latitude")
Arshid KV
  • 8,979
  • 3
  • 32
  • 35
27

You have a method called getArguments() that belongs to Fragment class.

Vasily Kabunov
  • 5,655
  • 12
  • 47
  • 49
codeScriber
  • 4,524
  • 6
  • 37
  • 62
8

in Frag1:

Bundle b = new Bundle();

b.putStringArray("arrayname that use to retrive in frag2",StringArrayObject);

Frag2.setArguments(b);

in Frag2:

Bundle b = getArguments();

String[] stringArray = b.getStringArray("arrayname that passed in frag1");

It's that simple.

Manaus
  • 397
  • 4
  • 9
Siva krishna
  • 179
  • 2
  • 7
6

Instantiating the Fragment the correct way!

getArguments() setArguments() methods seem very useful when it comes to instantiating a Fragment using a static method.
ie Myfragment.createInstance(String msg)

How to do it?

Fragment code

public MyFragment extends Fragment {

    private String displayMsg;
    private TextView text;

    public static MyFragment createInstance(String displayMsg)
    {
        MyFragment fragment = new MyFragment();
        Bundle args = new Bundle();
        args.setString("KEY",displayMsg);
        fragment.setArguments(args);           //set
        return fragment;
    }

    @Override
    public void onCreate(Bundle bundle)
    {
        displayMsg = getArguments().getString("KEY"):    // get 
    }

    @Override
    public View onCreateView(LayoutInlater inflater, ViewGroup parent, Bundle bundle){
        View view = inflater.inflate(R.id.placeholder,parent,false);
        text = (TextView)view.findViewById(R.id.myTextView);
        text.setText(displayMsg)    // show msg
        returm view;
   }

}

Let's say you want to pass a String while creating an Instance. This is how you will do it.

MyFragment.createInstance("This String will be shown in textView");

Read More

1) Why Myfragment.getInstance(String msg) is preferred over new MyFragment(String msg)?
2) Sample code on Fragments

Rohit Singh
  • 14,672
  • 7
  • 83
  • 77
2

for those like me who are looking to send objects other than primitives, since you can't create a parameterized constructor in your fragment, just add a setter accessor in your fragment, this always works for me.

Mina Gabriel
  • 19,881
  • 24
  • 93
  • 121
  • That's a wrong way. If a fragment recreates, it will lose those parameters. Parameters sent to the fragment should be serializable, passed through `setArguments()`. Nonserializable parameters can be set with setter, but it should again be called on activity/fragment recreate. – CoolMind Oct 06 '18 at 19:26
0

If you are using navigation components and navigation graph create a bundle like this

val bundle = bundleOf(KEY to VALUE) // or whatever you would like to create the bundle

then when navigating to the other fragment use this:

findNavController().navigate(
        R.id.action_navigate_from_frag1_to_frag2,
        bundle
    )

and when you land the destination fragment u can access that bundle using

Bundle b = getArguments()// in Java

or

val b = arguments// in kotlin
Mohammad Elsayed
  • 1,625
  • 1
  • 14
  • 38