How to pass data between multiple Fragments in Android

1.5k views Asked by At

In my main Activity, I have a DialogFragment that contains a FragmentTabHost. I have two tabs, one that is a DialogFragment and one that is a ListFragment. When either the 'OK' button is pressed in the inner DialogFragment or when an element in the ListFragment is pressed, I want to pass two Strings (that are entered in two TextView's in the inner DialogFragment and are displayed in each element in the ListFragment) back to the Activity, but I am unsure of how to do this with multiple levels of Fragments.

Any help is appreciated!

2

There are 2 answers

0
Youngjae On BEST ANSWER

There's no magic.

You can achieve with two approaches.

  1. Use callback.

Create interface and class to pass the data through child Fragment to Activity. You don't need to modify bridged TabHostFragment as Fragment always rely on its mother Context (Activity) no matter how many fragments wrap the fragment.

public class TwoStrings {
    public TwoStrings(String one, String two){
        this.one = one;
        this.two = two;
    }
    public String one;
    public String two;
}

First, declare interface.

public interface DataPassListener {
    void dataPassed(TwoStrings data);
}

And, implement interface in Activity.

public class MainActivity extends Activity implements DataPassListener {
    @Override
    public void dataPassed(TwoStrings data) {
        // do something with data.
        Log.d("string one", data.one);
        Log.d("string two", data.two);
    }
}

Finally, let child Fragment acknowlege that mother Activity has the callback listener.

public class DialogFragment1 extends DialogFragment {
    DataPassListener listener;

    @Override
    public void onAttach(Activity activity) {
        if (activity instanceOf DataPassListener)
            listener = (DataPassListener) activity;
    }

    public void setDataPassListener(DataPassListener listener){
        listener = ((DataPassListener) listener);
    }    

    public void doSomeThing(){
        if(listener != null) // important to prevent NullPointerException
            listener.dataPassed("a", "b");
    }
}
  1. Use EventBus.

I prefer to use Otto in order to publish and subscribe data.

To subscribe event for listening in Activity,

public class MainActivity extends Activity {

    @Override
    public void onResume() {
        super.onResume();
        BusProvider.getInstance().register(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        BusProvider.getInstance().unregister(this);
    }

    @Subscribe
    public void onUpdateTwoStrings(TwoStrings event) {
        // do something with data.
        Log.d("string one", data.one);
        Log.d("string two", data.two);
    }
}

And, publish event in anywhere in Fragment.

bus.post(new TwoStrings("a", "b"));
0
Stepango On

Take a look at setTargetFragment() and getTargetFragment() methods. You could connect fragments with each other through it without any additional callbacks and libs.