How do send a data from a fragment to the activity that creates the fragment?

I have an activity and I create a fragment when this activity runs. How do I get the data from the fragment to the activity that creates the fragment?

The part where I create an intent in my activity:

protected void onNewIntent(Intent intent) {

    RoutePlansFragment routePlansFragment = RoutePlansFragment.newInstance();
    FragmentTransactionUtil.addFragment(getSupportFragmentManager(), getFragmentContainerId(), routePlansFragment, routePlansFragment.getFragmentTag());


This is my fragment:

public static RoutePlansFragment newInstance() {
    RoutePlansFragment routePlansFragment = new RoutePlansFragment();
    return routePlansFragment;

How can I send data from this fragment into activity?

1 answer

  • answered 2018-09-24 15:20 Ahmed.ess

    you can use a callback

    in your fragment you have to create a listener

    public interface DataListener{
        void sendData(String data);

    create a reference

        private DataListener mDataListener;

    and in onAttach you have to caste your activity as DataListener

    public void onAttach(Context context) {
        if (context instanceof DataListener) {
            mDataListener = (DataListener) context;

    your activiy must implements DataListener

    and u can send data from fragment by calling sendData method