After my previous introductory post on Android fragments, the next natural step in the series would be to explain how to send information from a fragment to an activity.

This is not going to be a step-by-step tutorial like the previous one (it would be pointless to cover the project creation once again) but I will include all the necessary code to make fragment-to-activity communication work.

Suppose that you have a string inside your fragment that you want to pass to its parent activity.

String myString = "This is a string inside the fragment";

The Android documentation says the following about fragment to activity communication:

To allow a Fragment to communicate up to its Activity, you can define an interface in the Fragment class and implement it within the Activity. The Fragment captures the interface implementation during its onAttach() lifecycle method and can then call the Interface methods in order to communicate with the Activity.

Let’s break this down into a few steps, shall we?

  1. We are going to define the interface inside the fragment
  2. We are going to write the onAttach() method in the fragment
  3. We are going to implement this interface in the activity
  4. We are going to call this interface from the fragment to pass data to the parent activity.

Define an interface in the fragment

In your fragment class, add the following variable together with the other class variables, at the beginning of your class file:

private OnFragmentInteractionListener mListener;

Then, right at the end of your class file (after all the lifecycle methods such as onCreate(), onDetach() etc.), add something like this:

public interface OnFragmentInteractionListener {
    void onFragmentInteraction(String string);
}

We are going to define the onFragmentInteraction(String string) method later on.

Write the onAttach() method

The fragment needs to use the listener you have just defined inside its onAttach() method, as per the documentation above. So, let’s write it:

public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof OnFragmentInteractionListener) {
        mListener = (OnFragmentInteractionListener) context;
    } else {
        throw new RuntimeException(context.toString()
        + " must implement OnFragmentInteractionListener");
    }
}

Now that we are here, we might as well write the onDetach() method too:

public void onDetach() {
    super.onDetach();
    mListener = null;
}

This doesn’t do much, it simply nullifies the listener.

Implement the interface within the activity

This is quick and painless. You just have to make sure that your parent activity implements the interface you have just defined. So, if your activity is called MainActivity, you should do this:

public class MainActivity extends AppCompatActivity implements YourFragment.OnFragmentInteractionListener

Then, still inside your main activity, implement OnFragmentInteractionListener so you can receive the String coming from the fragment and do something with it. In the following example, we are just going to save this string inside a variable of the activity.

public void onFragmentInteraction(String string) {
    this.myString = string;
}

Now the main activity is ready to receive the String and to do something with it (in this case, saving it to the MainActivity’s myString variable), the only thing that left to do is to actually send the string from the fragment.

Send information from the fragment

Remember the mListener variable you created earlier? That’s what we are going to use to send our string to the parent activity. At this point, the biggest part of the job is done

Now, wherever you need to (i.e. wherever you want your fragment to send information to your main activity), you can just run this:

mListener.onFragmentInteraction(myString);

Now try outputting your string in Logcat from your activity and you should see “This is a string inside the fragment”.