【发布时间】:2016-12-17 08:15:12
【问题描述】:
我有一个使用 Viewpager 的活动。我的浏览器有几个片段。我想在选择 Otto 事件时将其发送到片段,因此我实现了ViewPager.OnPageChangeListener
@Override
public void onPageSelected(int position) {
currentPosition = position;
switch (position){
case 0:
EventBus.getInstance().post(new TypeEvent());
break;
case 1:
EventBus.getInstance().post(new InternalEvent());
break;
}
}
在我的第一个片段中
@Override
public void onStart() {
super.onStart();
EventBus.getInstance().register(this);
}
@Override
public void onStop(){
super.onStop();
EventBus.getInstance().unregister(this);
}
@Subscribe
public void init(TypeEvent event){
Logger.d("type event received");
//do something.......
}
我的事件总线类
public class EventBus extends Bus {
private static EventBus eventBus;
public static EventBus getInstance(){
if(eventBus == null){
eventBus = new EventBus();
}
return eventBus;
}
}
问题是我的片段没有接收到事件。可能是什么问题?
【问题讨论】:
-
调用 post 方法时触发事件?
-
@sasikumar 我知道了问题的原因。在下面查看我的答案
标签: android android-fragments event-bus otto