您的位置:首页 > 其它

EventBus使用中遇到的问题

2016-11-24 15:47 92 查看
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());
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  EventBus 发布订阅