How to pass data between multiple Fragments in Android

筅森魡賤 提交于 2019-12-08 07:33:23

问题


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!


回答1:


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"));



回答2:


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



来源:https://stackoverflow.com/questions/30771668/how-to-pass-data-between-multiple-fragments-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!