片段之间进行通信的最佳方式是什么?

时间:2014-09-22 14:03:01

标签: android android-activity fragment

我有一个片段容器,它有3个不同的片段,我想在3片段之间通知消息

最好的方法是什么?

由于

3 个答案:

答案 0 :(得分:1)

最简单的&最好的方法是使用一些事件总线库。它将保持您的代码清洁和片段,活动之间松散耦合的通信。

Otto 非常容易设置&使用。它来自sqaure。请查看@ http://square.github.io/otto/

http://corner.squareup.com/2012/07/otto.html也会让你一瞥它解决了什么问题。

答案 1 :(得分:0)

在Activity中保留一个公共变量(如果是小数据),或者在Activity中编写一个接口并在所有片段中实现该接口。并且使用接口通过在其中编写方法来传递数据。

答案 2 :(得分:0)

你走了。这是做片段的正确和完整的方式< - >活动沟通。片段< - >片段通信应该通过控制它们的活动。我添加了相关的接口,以onBackPress为例:

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

}