Home » Android » How to pass a variable from Activity to Fragment, and pass it back?

How to pass a variable from Activity to Fragment, and pass it back?

Posted by: admin November 29, 2017 Leave a comment

Questions:

I am currently making an android app, and I want to pass a date between activity and fragment.
My activity has a button, which opens the fragment: DatePickerFragment.

On my activity I show a date, which I want to modify with the fragment. So I want to pass the date to the datepicker, and send it back to the activity.

I’ve tried alot of solutions, but none are working. The easy way would be pass an argument, but this can’t be done with fragments.

Answers:

To pass info to a fragment , you setArguments when you create it, and you can retrieve this argument later on the method onCreate or onCreateView of your fragment.

On the newInstance function of your fragment you add the arguments you wanna send to it:

/**
 * Create a new instance of DetailsFragment, initialized to
 * show the text at 'index'.
 */
public static DetailsFragment newInstance(int index) {
    DetailsFragment f = new DetailsFragment();
    // Supply index input as an argument.
    Bundle args = new Bundle();
    args.putInt("index", index);
    f.setArguments(args);
    return f;
}

Then inside the fragment on the method onCreate or onCreateView you can retrieve the arguments like this:

Bundle args = getArguments();
int index = args.getInt("index", 0);

If you want now communicate from your fragment with your activity (sending or not data), you need to use interfaces. The way you can do this is explained really good in the documentation tutorial of communication between fragments. Because all fragments communicate between each other through the activity, in this tutorial you can see how you can send data from the actual fragment to his activity container to use this data on the activity or send it to another fragment that your activity contains.

Documentation tutorial:

http://developer.android.com/training/basics/fragments/communicating.html

Questions:
Answers:

Sending data from Activity to a Fragment

Activity:

Bundle bundle = new Bundle();
String myMessage = "Stackoverflow is cool!";
bundle.putString("message", myMessage );
FragmentClass fragInfo = new FragmentClass();
fragInfo.setArguments(bundle);
transaction.replace(R.id.fragment_single, fragInfo);
transaction.commit();

Fragment:

Reading the value in fragment

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    String myValue = this.getArguments().getString("message");
    ...
    ...
    ...
}

But if you want to send values from Fragment to Activity, read the answer of jpardogo, you must need interfaces, more info: Communicating with other Fragments

Questions:
Answers:

Use the library EventBus to pass event that could contain your variable back and forth. It’s a good solution because it keeps your activities and fragments loosely coupled

https://github.com/greenrobot/EventBus