事件总线分发库EventBus详解

来源:互联网 发布:桥接模式 java 编辑:程序博客网 时间:2024/05/10 13:21

前言

EventBus是一个代替Intent,Handler和Broadcast Receiver在Activity,Fragment和Service之间进行数据传递的开源库。
github地址:https://github.com/greenrobot/EventBus

简介

包含4个部分:发布者,订阅者,事件,总线。
步骤:订阅——>注册——>发布——>取消注册
这里写图片描述

使用

具体的使用还是比较清晰简单的,看了鸿洋大神的博客,写的比较详细。

Android EventBus实战 没听过你就out了
http://blog.csdn.net/lmj623565791/article/details/40794879
Android EventBus源码解析 带你深入理解EventBus
http://blog.csdn.net/lmj623565791/article/details/40920453

需要注意的问题:

现在EventBus版本为org.greenrobot:eventbus:3.0.0,我们在接受消息的时候,在接收方法
onEvent()onEventMainThread()onEventAsync(),onEventBackgroundThread()要加上注解@Subscribe,导包import org.greenrobot.eventbus.Subscribe;,否则会报错!
而且,对于接收消息的方法也不局限于这四种,可以自定义方法名!

1 0