Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing data from one fragment to another

I have an Activity with two fragments and I need to pass a string from FragmentA to FragmentB.

To pass the data, I have this in my FragmentA:

    Intent intent = new Intent(getActivity(), FragmentB.class);
    intent.putExtra("name", "Mark");
    startActivity(intent);

And to get the data, I did this in FragmentB

    Intent intent = getActivity().getIntent();
    Bundle b = intent.getExtras();

    if(b!=null)
    {
        String name =(String) b.get("name");
        nameTextView.setText(name);
    }

But this is not working. Is there a specific way to pass a string from one fragment to another fragment?

like image 395
Night Avatar asked Mar 21 '14 19:03

Night


People also ask

How do I send text from one fragment to another?

So to share a string between fragments you can declare a static String in Activity. Access that string from Fragment A to set the value and Get the string value in fragment B. 2. Both fragments are hosted by different Activities- Then you can use putExtra to pass a string from Fragment A of Activity A to Activity B.

How do you pass data between fragments and activities?

So, to pass data from the MotherActivity to such a Fragment you will need to create private Strings/Bundles above the onCreate of your Mother activity - which you can fill with the data you want to pass to the fragments, and pass them on via a method created after the onCreate (here called getMyData()).


3 Answers

  1. If fragments are hosted by same activity- You cannot cast an intent to Fragment. Fragment acts as a part of Activity, it is not an activity by itself. So to share a string between fragments you can declare a static String in Activity. Access that string from Fragment A to set the value and Get the string value in fragment B.
  2. Both fragments are hosted by different Activities- Then you can use putExtra to pass a string from Fragment A of Activity A to Activity B. Store that string in Activity B and use it in Fragment B.
like image 164
J.Ajendra Avatar answered Oct 22 '22 06:10

J.Ajendra


to pass data between Fragments you can use setArguments(Bundle b). For instance:

public class FragmentA extends Fragment {

  public static FragmentA newInstance(String name) {
    Bundle bundle = new Bundle();
    bundle.putString("name", name);
    FragmentA f = new FragmentA(); 
    f.setArguments(bundle);
    return f;
  }

}
like image 23
Blackbelt Avatar answered Oct 22 '22 06:10

Blackbelt


You can do something like below,

 Fragment fr=new friendfragment();
 FragmentManager fm=getFragmentManager();
 android.app.FragmentTransaction ft=fm.beginTransaction();
 Bundle args = new Bundle();
 args.putString("CID", "your value");
 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);
}
like image 40
Demian Flavius Avatar answered Oct 22 '22 07:10

Demian Flavius