Android_programmer_camera
Android_programmer_camera

Reputation: 13369

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

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?

Upvotes: 106

Views: 180648

Answers (7)

ashakirov
ashakirov

Reputation: 12360

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

Upvotes: 197

Mohammad Elsayed
Mohammad Elsayed

Reputation: 2066

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

Upvotes: 0

Siva krishna
Siva krishna

Reputation: 207

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.

Upvotes: 8

Rohit Singh
Rohit Singh

Reputation: 18222

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

Upvotes: 6

codeScriber
codeScriber

Reputation: 4612

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

Upvotes: 27

Mina Gabriel
Mina Gabriel

Reputation: 25110

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.

Upvotes: 2

Arshid KV
Arshid KV

Reputation: 10037

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

Upvotes: 43

Related Questions