Question Detail

how to transfer data between fragments Android?

5 years ago Views 1296 Visit Post Reply

I have Fragments in Activity, I know how to data pass in Activity to Activity but I am not able to pass data between fragment to fragment.

Thread Reply

Bili Greed

- 5 years ago

From Activity, you can send data to Fragment with intent as:

Bundle bundle=new Bundle();
bundle.putString("KEY", "Message_here"); //set Fragment Arguments
FirstFragment fragment=new FirstFragment();

and to receive in fragment in Fragment onCreateView method:

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
          String strMsg=getArguments().getString("KEY");

Toast.makeText(getActivity(),"Message : "+strMsg,Toast.LENGTH_SHORT).show();
    return inflater.inflate(R.layout.fragment, container, false);


- 5 years ago

Use Bundle to send String:

//Put the value
OneFragment frg= new AnotherFragment ();
Bundle bundle = new Bundle();
bundle.putString("KEY", "STRING VALUE HERE");

//Inflate the fragment
getFragmentManager().beginTransaction().add(, frg).commit();


In onCreateView of the new Fragment:

//Retrieve the value
String value = getArguments().getString("KEY");