Question Detail

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

- 1 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();
fragment.setArguments(bundle);

and to receive in fragment in Fragment onCreateView method:

@Override
    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);
    }

Anonymous

- 11 months ago

Use Bundle to send String:

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

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

 

In onCreateView of the new Fragment:

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