41

I am using fragments in my application. This is my first fragment that simply inflate a 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, that contains a 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.

Adinia
  • 3,722
  • 5
  • 40
  • 58
Vikky
  • 933
  • 2
  • 15
  • 29

4 Answers4

81

In your FragmentA fragment set the bundle as the argument.

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

In your ListFrag fragment get the bundle as

Bundle b = getArguments();
int s = b.getInt("doctor_id");
noelicus
  • 14,468
  • 3
  • 92
  • 111
Tarun
  • 13,727
  • 8
  • 42
  • 57
  • but i just loading the left_panel.xml file in that xml file i give the Fragment name... this is the method that i calling another fragment... here ur answer is suitable ?? – Vikky Jun 12 '13 at 10:58
  • Check out `onInflate()`. create your own attributes that get copied into the arguments.bundle. [link](http://developer.android.com/reference/android/app/Fragment.html#onInflate) – Tarun Jun 12 '13 at 11:08
  • There is an option to set arguments to xml inflated fragment via onInflate function. I gave the link to the function example in the above comment. – Tarun Jun 12 '13 at 12:31
  • @Tarun onInflate gets arguments from the XML layout, not from the Activity or Fragment inflating the layout, which is what I believe the Vikky was asking. – Pierre-Luc Paour Sep 13 '13 at 07:11
  • 8
    This code is wrong. `String s = b.getInt("doctor_id");` needs to be changed to `int s = b.getInt("doctor_id");` Small error. +1 though – Chad Bingham Mar 03 '14 at 23:02
  • This answer makes sense if the fragmentB can be loaded from fragmentA. If requirement is to always load the fragments from Activity, or to load the fragment dynamically i.e. fragmentB can be loaded sometimes from A or from C etc., it makes more sense to use the interface approach. THe method of interface is implemented in activity and also all of the fragments implement this interface. – Rusheel Jain Mar 21 '16 at 13:58
17

Fragment to Fragment set and get Argument:

Start Activity :

     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 this may be this sample help you.

sherin
  • 1,091
  • 2
  • 17
  • 27
  • And a full fragment how you can do it ? –  Mar 10 '16 at 08:26
  • @delive I didn't get you ? can you make your question clear ? – sherin Mar 10 '16 at 09:25
  • @delive Fragment A-----"pass"----function_name(Data)--> FragmentActivity -----"get"----function_name(string data) ------>This is a Cycle – sherin Mar 10 '16 at 09:32
  • public void getFragment(Fragment frag){} , I have now : Class fragmentClass = fragmentCurrent; Fr f = (Fr) fragmentClass.newInstance(), but always get a new instance ... –  Mar 10 '16 at 10:09
1

Create a static method of ListFrag in ListFrag such as:

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 would call:

Fragment frag = ListFrag.newInstance(this.doctor_id);
// use frag with getSupportChildFragmentManager();
gunar
  • 14,660
  • 7
  • 56
  • 87
1

You have two choice :

A. If i you have reference of your ListFragment You can set target fragment for FragmentA by doing this :

  1. in FragmentA this.setTargetFragment(yourListFragment);
  2. then this.getTargetFragment().setArguments(yourBundle);
  3. and in ListFragment get it back with in with this.getArguments();

B. The most logic way

I bet your fragment are in the same activity so she have references of them. You can pass data to your activity from FragmentA and pass it to ListFragment

FragmentA --Data--> FragmentActivity --Data--> ListFragment

Glenn Sonna
  • 1,853
  • 3
  • 19
  • 25