1.在订阅的页面onCreate中进行注册
EventBus.getDefault().register(this);
2.同时在onDestory中解除注册
@Override
public void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
3.重写event(onEvent、
onEventMainThread、onEventAsync、onEventBackgroundThread)
4.在需要的地方发布订阅
EventBus.getDefault().post(new FirstEvent("success"));
EventBus 3.0可能出现异常
FATAL EXCEPTION: main
Process: com.hank.rxjavademo, PID: 6329
org.greenrobot.eventbus.EventBusException: Subscriber class com.hank.fragment.map.MapFragment and its super classes have no public methods with the @Subscribe annotation
at org.greenrobot.eventbus.SubscriberMethodFinder.findSubscriberMethods(SubscriberMethodFinder.java:67)
at org.greenrobot.eventbus.EventBus.register(EventBus.java:136)
at com.hank.fragment.map.MapFragment.onCreateView(MapFragment.java:34)
解决方案:
在重写的event方法前加注解 @Subscribe
@Subscribe
public void onEventMainThread(FirstEvent event){
eventBusButton.setText(event.getMsg());
}