What is the best way to communication between frag

2019-08-23 22:34发布

问题:

I have a fragment container that holed 3 different fragments and i want to notify message between the 3 fragment

What is the best way to do it?

Thanks

回答1:

The simplest & best approach will be use of some event bus library. It will keep your code clean & loosely coupled communications between fragments,activities.

Otto is extremely easy to setup & use. It is from sqaure. Check it out at @ http://square.github.io/otto/

http://corner.squareup.com/2012/07/otto.html will also give you glimpse of what problem it solves.



回答2:

Keeping a common variable in Activity(if small data) or, write an interface in Activity and implement that in all fragments. And using interfaces pass the data, by writing methods in it.



回答3:

Here you go. this is the right and full way of doing fragment <-> activity communication. fragment <-> fragment communication should passthrough the activity who controls them. I added the relevant interfaces, with onBackPress as an example:

 public class BaseFragment extends Fragment implements BaseActivityDelegate {

    private BaseActivityInterface mActivity;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        if (mActivity != null) {
            mActivity.registerBaseActivityDelegate(this);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mActivity != null) {
            mActivity.unregisterBaseActivityDelegate(this);
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mActivity = (BaseActivityInterface)activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() +" must implement BaseActivityInterface");
        }
    }

    @Override
    public void onDetach () {
        super.onDetach();
        mActivity = null;
    }

    /**
     * Override by sub-class in order to intercept the back press.
     *
     * @return true if the back press is consume by the fragment
     */
    @Override
    public boolean onBackPressed() {
        return false;
    }
}




public class BaseActivity extends FragmentActivity implements BaseActivityInterface {

    protected ArrayList<BaseActivityDelegate> mBaseActivityDelegates;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.mBaseActivityDelegates = new ArrayList<BaseActivityDelegate>();
    }

    @Override
    public void registerBaseActivityDelegate(BaseActivityDelegate delegate) {
        if (mBaseActivityDelegates != null) {
            mBaseActivityDelegates.add(delegate);
        }
    }

    @Override
    public void unregisterBaseActivityDelegate(BaseActivityDelegate delegate) {
        if (mBaseActivityDelegates != null) {
            mBaseActivityDelegates.remove(delegate);
        }
    }

    @Override
    public void onBackPressed() {
        if (mBaseActivityDelegates != null) {
            for (BaseActivityDelegate delegate : mBaseActivityDelegates) {
                if (delegate != null && delegate.onBackPressed()) {
                    // the delegate intercepted the back press event
                    return;
                }
            }
        }
        // back press was not intercepted, continue handling it
        super.onBackPressed();
    }
}


public interface BaseActivityDelegate {
    public boolean onBackPressed();
}

public interface BaseActivityInterface {
    public void registerBaseActivityDelegate(BaseActivityDelegate delegate);
    public void unregisterBaseActivityDelegate(BaseActivityDelegate delegate);

}