106

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?

7 Answers 7

197

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");   
     }
}

EDIT:

Best practice is read and save arguments in onCreate method. It's worse to do it in onCreateView because onCreateView will be called each time when fragment creates view (for example each time when fragment pops from backstack)

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Bundle arguments = getArguments(); 
}
5
  • 12
    It is returning null in my case, any idea why this is happening ?
    – Anirudh
    Commented Apr 1, 2013 at 10:09
  • 2
    You're putting ArrayList into the bundle, but getting a String Array. You should do bundle.getStringArrayList("elist");
    – Rafał
    Commented Oct 14, 2014 at 10:18
  • 1
    You forgot the return statement: return super.onCreateView(inflater, container, savedInstanceState);
    – user41805
    Commented Jan 1, 2016 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. Commented Jan 29, 2016 at 3:54
  • 5
    @almaz_from_kazan @HabeebPerwad Why are you using getArguments() in onCreateView, not in onCreate?
    – Nik Kober
    Commented May 23, 2016 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")
27

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

0
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.

0
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

1
  • android.os.Bundle doesn't have setString. Do you mean putString() ? Commented Nov 11, 2019 at 15:37
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.

1
  • 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
    Commented Oct 6, 2018 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

Not the answer you're looking for? Browse other questions tagged or ask your own question.