How to pass a bundle from fragment to fragment

I use fragments in my application. This is my first snippet that simply inflates an XML file:

public class FragmentA extends SherlockFragment { Context myContext,appContext; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub myContext = getActivity(); appContext=getActivity().getApplicationContext(); arguments = getArguments(); doctor_id=arguments.getInt("doctor_id"); userType=arguments.getString("userType"); return inflater.inflate(R.layout.left_panel, container,false); } 

and this is the left_panel.xml file containing the fragment:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <fragment android:id="@+id/titles" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" class="com.example.sample.ListFrag" /> </LinearLayout> 

This is my ListFrag class:

 public class ListFrag extends Fragment { Context myContext,appContext; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub layoutView = inflater.inflate(R.layout.activity_doctor_list,container); myContext = getActivity(); appContext=getActivity().getApplicationContext(); arguments=getArguments(); int doctor_id=arguments.getInt("doctor_id"); } } 

I don't know how to pass Bundle arguments from FragmentA to ListFrag.

+23
android android-fragments
Jun 12 '13 at 10:37
source share
4 answers

In your FragmentA fragment, set the package as an argument.

 Bundle args = new Bundle(); args.putInt("doctor_id",value); ListFrag newFragment = new ListFrag (); newFragment.setArguments(args); 

In your ListFrag snippet get the package as

 Bundle b = getArguments(); int s = b.getInt("doctor_id"); 
+56
Jun 12 '13 at 10:49 on
source share

Fragment to determine the fragment and get the argument:

Beginning of work:

  int friendId = 2; //value to pass as extra i = new Intent(firstActivity, SecondActivity.class); i.putExtra("friendsID", friendId); firstActivity.startActivity(i); 

SecondActivity:

  Fragment_A mFragment_A = new Fragment_A(); mFragment_A.setArguments(getIntent().getExtras()); 

Fragment_a:

  Bundle bundle = new Bundle(); String Item = getArguments().getString("friendsID"); bundle.putInt("friendsID", Integer.parseInt(Item)); // code Fragment_B mFragment_B = new Fragment_B(); mFragment_B.setArguments(bundle); 

Fragment_b:

  Bundle bundle = getArguments(); int value = bundle.getInt("friendsID"); Log.e("value Fragment get Argument ", "friendsID :" + value); 

this work for me, try it, maybe this sample will help you.

+13
Feb 13 '14 at 7:28
source share

Create a static ListFrag method in ListFrag, for example:

 public static ListFrag newInstance(int doctorId) { ListFrag frag = new ListFrag(); Bundle args = new Bundle(); args.putExtra("doctor_id", doctorId); frag.setArguments(args); return frag; } 

When you create a ListFrag from fragment A, you call:

 Fragment frag = ListFrag.newInstance(this.doctor_id); // use frag with getSupportChildFragmentManager(); 
0
Jun 12 '13 at 10:45
source share

You have two options:

but. If you have a link to your ListFragment, you can set the target fragment for FragmentA by doing the following:

  • in FragmentA this.setTargetFragment(yourListFragment);
  • then this.getTargetFragment().setArguments(yourBundle);
  • and in ListFragment return it using this.getArguments();

B. The most logical way

I bet your fragment is in the same activity, so it has links to them. You can transfer data to your activity with FragmentA and transfer it to the Fragment List

FragmentA --Data → FragmentActivity --Data → ListFragment

0
Jun 12 '13 at 11:29
source share



All Articles