在活动中我这样做:
onCreate()
:
EventBus.getDefault().register(this);
我覆盖了这个:
@Override
protected void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}
@Override
protected void onStop() {
EventBus.getDefault().unregister(this);
super.onStop();
}
在我的服务中我做到了这一点:
Global.getInstance().getGeoLocation().setBestLocation(loc);
EventBus.getDefault().post(new EventLocation(loc));
在logd中我看到了:
10-27 09:15:04.367 5097-5097/komunal W/EventBus: Subscriber to unregister was not registered before: class komunal.activities.PGOListActivity
10-27 09:15:04.458 5097-5097/komunal D/EventBus: No subscribers registered for event class komunal.events.EventLocation
10-27 09:15:04.458 5097-5097/komunal D/EventBus: No subscribers registered for event class org.greenrobot.eventbus.NoSubscriberEvent
10-27 09:15:05.425 5097-5097/komunal D/EventBus: No subscribers registered for event class komunal.events.EventLocation
10-27 09:15:05.425 5097-5097 komunal D/EventBus: No subscribers registered for event class org.greenrobot.eventbus.NoSubscriberEvent
10-27 09:15:06.458 5097-5097/komunal D/EventBus: No subscribers registered for event class komunal.events.EventLocation
10-27 09:15:06.459 5097-5097/komunal D/EventBus: No subscribers registered for event class org.greenrobot.eventbus.NoSubscriberEvent
10-27 09:15:07.435 5097-5097/komunal D/EventBus: No subscribers registered for event class komunal.events.EventLocation
10-27 09:15:07.436 5097-5097/komunal D/EventBus: No subscribers registered for event class org.greenrobot.eventbus.NoSubscriberEvent
答案 0 :(得分:2)
首先,您只需要注册一次订阅者。因此,如果您已在onCreate
注册,请勿在{{1}}重新注册。
其次,只要您的活动不再可见,您的活动就会被取消订阅。这是因为您有以下内容:
onStart()
相反,您需要将取消注册移至@Override
protected void onStop() {
EventBus.getDefault().unregister(this);
super.onStop();
}
:
onDestroy()
最后,您需要使用以下内容订阅活动中的@Override
protected void onDestroy() {
EventBus.getDefault().unregister(this);
super.onDestroy();
}
:
Event