user3256145 user3256145 - 2 years ago 109
Android Question

How to pass a value from one Fragment to another in Android?

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(); ft = getFragmentManager().beginTransaction();
ft.replace(, fr);

Answer Source

You can do something like below,

 String cid=id.getText().toString();
 Fragment fr=new friendfragment();
 FragmentManager fm=getFragmentManager(); ft=fm.beginTransaction();
 Bundle args = new Bundle();
 args.putString("CID", cid);
 ft.replace(, fr);

To receive the data do the following,

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    String strtext = getArguments().getString("CID");    
    return inflater.inflate(R.layout.fragment, container, false);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download