0

From the activity, I open the fragment "DevicesFragment" as below. How do I transfer data from activity to fragment before starting it? Thanks for help !

getSupportFragmentManager().beginTransaction().add(R.id.fragment, new DevicesFragment(), "devices").commit();

1 Answers1

0

From Activity you send data with intent as:

Bundle bundle = new Bundle();
bundle.putString("text", "From Activity");
// set Fragmentclass Arguments
DevicesFragment devices = new DevicesFragment();
devices.setArguments(bundle);
getSupportFragmentManager().beginTransaction().add(R.id.fragment, devices, "devices").commit();

and in DevicesFragment onCreateView method:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    Bundle bundle = getArguments();
    if(bundle != null) {
        String value = bundle.getString("text");  
    }  
    return inflater.inflate(R.layout.fragment, container, false);
}
Atick Faisal
  • 1,086
  • 1
  • 6
  • 13