EventBus框架使用

EventBus是一个针对Android优化的发布/订阅事件总线的框架。意思就是无论你是Activity间通信,fragment间通信,activity与fragment间通信都可以使用EventBus.
1.在项目中添加EventBus
Gradle:
compile 'org.greenrobot:eventbus:3.0.0'
Maven:

  <dependency>
          <groupId>org.greenrobot</groupId>
          <artifactId>eventbus</artifactId>
          <version>3.0.0</version>
  </dependency>

2.自定义一个事件类

public class AnyEventType{
      public AnyEventType(){}
}

3.在接收消息的页面注册

EventBus.getDefault().register(this);

4.接收消息的方法

@Subscrible
public void onEvent(AnyEventType event){

/*do something */
}
  1. 发送消息
EventBus.getDefault().post(event);
  1. 在接收消息的页面取消注册
EventBus.getDefault().unregister(this);

参考:https://github.com/greenrobot/EventBus
http://www.jianshu.com/p/a040955194fc

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容