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

7

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?

Anatol answered 24/3, 2011 at 21:10 Comment(0)
C
27

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

Cabanatuan answered 24/3, 2011 at 22:7 Comment(0)
S
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(); 
}
Sanskrit answered 21/8, 2012 at 8:14 Comment(5)
It is returning null in my case, any idea why this is happening ?Mammalogy
You're putting ArrayList into the bundle, but getting a String Array. You should do bundle.getStringArrayList("elist");Euphemism
You forgot the return statement: return super.onCreateView(inflater, container, savedInstanceState);Rennold
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.Benz
@almaz_from_kazan @HabeebPerwad Why are you using getArguments() in onCreateView, not in onCreate?Xray
M
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")
Milford answered 10/12, 2015 at 10:14 Comment(0)
C
27

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

Cabanatuan answered 24/3, 2011 at 22:7 Comment(0)
D
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.

Dufour answered 27/3, 2017 at 6:6 Comment(0)
N
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

Null answered 3/1, 2019 at 18:14 Comment(1)
android.os.Bundle doesn't have setString. Do you mean putString() ?Yabber
L
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.

Laodicean answered 7/7, 2016 at 15:13 Comment(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.Fukuoka
T
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
Triggerhappy answered 15/1, 2021 at 8:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.