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 */
}
- 发送消息
EventBus.getDefault().post(event);
- 在接收消息的页面取消注册
EventBus.getDefault().unregister(this);
参考:https://github.com/greenrobot/EventBus
http://www.jianshu.com/p/a040955194fc