Question

I am a newbie to Fragments. I want to pass a String value from one Fragment to another. how to do this? I have called my Fragments in the following way. please guide me step by step.

String cid = id.getText().toString();
Fragment fr = new FriendFragment();
android.app.FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fr);
ft.commit(); 
Was it helpful?

Solution

You can do something like below,

 String cid=id.getText().toString();
 Fragment fr=new friendfragment();
 FragmentManager fm=getFragmentManager();
 android.app.FragmentTransaction ft=fm.beginTransaction();
 Bundle args = new Bundle();
 args.putString("CID", cid);
 fr.setArguments(args);
 ft.replace(R.id.content_frame, fr);
 ft.commit(); 

To receive the data do the following,

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    String strtext = getArguments().getString("CID");    
    return inflater.inflate(R.layout.fragment, container, false);
}

OTHER TIPS

If you want to send data from fragment to activity you may use an interface.

But when you want to send data from fragment to another fragment it get's complicated. You would want to send data to activity and then to the other fragment.

I use EventBus to solve this problem. How it works.

  1. Create an event-E.
  2. From Fragment-A register for events.
  3. From fragment-B publish event-E with data you want to pass.
  4. You would get the data in onEvent() method you wrote in Fragment-A.

That's it. No need to write your own interfaces.

You may use eventbus for communications from background service or threads to activity also.

Checkout the EventBus HowTo and Repository also.

Fragment to Fragment communication must be done through the parent Activity.

FragmentToSendData

interface <interfaceName>{
    void <abstract method>(String str);
}

@Override
public void onAttach(Activity activity) {
        super.onAttach(activity);
       try{
           <instance of the interface> = (<interface>)getActivity();
       }catch (ClassCastException e) {
           throw new ClassCastException(getActivity().toString()
                   + " must implement <interface>");
       }

    }

ActivityWithBothFragments (this can be through ViewPager, or use the id of your fragment, just use findFragmentById() )

@Override <abstract method from frag1>(String str){
  FragmentToReceiveData fragment2 =  (FragmentToReceiveData)getSupportFragmentManager().findFragmentByTag(ViewPagerAdapter.getFragmentTag(1));                                                                                                                                                

    fragment2.getStringFromFrag1(String str);
}

FragmentToReceiveData

public void getStringFromFrag1(String str){
  <textview>.setText("str");

}
try this :

- send data : 

        Bundle arg = new Bundle();
        arg.putInt("ID", "12");  //arg.putInt("KEY", "VALUE")
        arg.putString("NAME","Jaydeep");//arg.putString("KEY","VALUE")

        YourFragment fragment = new YourFragment();
        fragment.setArguments(arg);

        fragmentManager.beginTransaction().add("PUT YOUR FRAM ID", fragment, fragment.getClass().getName())
        .addToBackStack(null)
        .commit();

- receive data : 

        Bundle bundle = getIntent().getExtras();
        bundle.getInt("ID");
        bundle.getString("NAME");

        Log.e("Receive data : " ,"\nID - "+bundle.getInt("ID")+"\nNAME - "+bundle.getString("NAME"));

you can try this

First Fragment

//adding data to the bundle class
Bundle b = new Bundle();
b.putString("user_name","simon");
b.putString("user_address","nepal");
//fragment operation
FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fragment fragment=new friendfragment();
fragment.setArguments(b);
transaction.replace(R.id.container, fragment);
transaction.addToBackStack(null);
transaction.commit();

//Second Fragment(Receiving Fragment)
//write following code into onCreateView() method
Bundle b = getArguments();
String name = b.getString("user_name");
String address = b.getString("user_address");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top