EventBus 3.0 封装

封装思路

  • 封装一个EventBus工具类,消息类MessageEvent
  • 在BaseActivity中选择注册及接收消息,默认不注册
  • 子Activity继承BaseActivity,如需接收消息,则重写父类的方法,返回true
  • 在需要发送消息的地方使用工具类发送消息

具体步骤

封装EventBusUtil类

public class EventBusUtil {
    public static void register(Object subscriber) {
        EventBus.getDefault().register(subscriber);
    }

    public static void unregister(Object subscriber) {
        EventBus.getDefault().unregister(subscriber);
    }

    public static void sendEvent(MessageEvent event) {
        EventBus.getDefault().post(event);
    }

    public static void sendStickyEvent(MessageEvent event) {
        EventBus.getDefault().postSticky(event);
    }
}

封装MessageEvent

public class MessageEvent<T> {
    private int code;
    private T data;

    public MessageEvent(int code) {
        this.code = code;
    }

    public MessageEvent(int code, T data) {
        this.code = code;
        this.data = data;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}

其中code对应不同的消息,data为发送的内容

在BaseActivity中选择注册及接收消息,默认不注册

public class BaseActivity extends Activity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (isRegisterEventBus()) {
            EventBusUtil.register(this);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (isRegisterEventBus()) {
            EventBusUtil.unregister(this);
        }
    }

    /**
     * 是否注册事件分发
     *
     * @return true绑定EventBus事件分发,默认不绑定,子类需要绑定的话复写此方法返回true.
     */
    protected boolean isRegisterEventBus() {
        return false;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventBusCome(MessageEvent event) {
        if (event != null) {
            receiveEvent(event);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onStickyEventBusCome(MessageEvent event) {
        if (event != null) {
            receiveStickyEvent(event);
        }
    }

    /**
     * 接收到分发到事件
     *
     * @param event 事件
     */
    protected void receiveEvent(MessageEvent event) {

    }

    /**
     * 接受到分发的粘性事件
     *
     * @param event 粘性事件
     */
    protected void receiveStickyEvent(MessageEvent event) {

    }
}

子Activity继承BaseActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ebfirst);
    ButterKnife.bind(this);
}

@OnClick(R.id.button)
public void onViewClicked() {
    EventBusUtil.sendStickyEvent(new MessageEvent<>(MyEventCode.CODE_B, mEditText.getText().toString()));
    startActivity(new Intent(this, SecondActivity.class));
}

@Override
protected boolean isRegisterEventBus() {
    return true;
}

@Override
protected void receiveEvent(MessageEvent event) {
    switch (event.getCode()) {
        case MyEventCode.CODE_A:
            String data = (String) event.getData();
            mTextView.setText(data);
            break;
    }
}

@Override
protected void receiveStickyEvent(MessageEvent event) {
    switch (event.getCode()) {

    }
}

版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/03/18/eventbus-3-encapsulation/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
海报
EventBus 3.0 封装
封装思路 封装一个EventBus工具类,消息类MessageEvent 在BaseActivity中选择注册及接收消息,默认不注册 子Activity继承BaseActivity,如需接收消息,则重写……
<<上一篇
下一篇>>
文章目录
关闭
目 录