I have an issue with EventBus
from Greenrobot.
I was trying to post an event from a background service form my sync adapter and catch it in a fragment to update the UI.
The problem is that when I try to post the event from sync adapter I have get the following in the debug log:
No subscribers registered for event class olexiimuraviov.ua.simplerssreader.event.UpdateUIEvent No subscribers registered for event class org.greenrobot.eventbus.NoSubscriberEvent
I register fragment in onResume
and unregister it in onPause
@Override
public void onResume() {
super.onResume();
EventBus.getDefault().register(this);
if (mDebug) Log.d(LOG_TAG, EventBus.getDefault().isRegistered(this) + "");
}
@Override
public void onPause() {
super.onPause();
EventBus.getDefault().unregister(this);
}
Log statement in onResume shows that fragment is successfully registered.
Here is onEvent method:
@Subscribe
public void onEvent(UpdateUIEvent event) {
if (mSwipeRefreshLayout.isRefreshing())
mSwipeRefreshLayout.setRefreshing(false);
}
I was trying to call onEvent method with Background threadmode but it didn't help.
After this I was trying to use handler to post event but eventbus still can't find any registered subscribers for event.
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
EventBus.getDefault().post(new UpdateUIEvent());
}
});
Here is my onPerform method of sync adapter:
@Override
public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult) {
// Fetch data from server
} finally {
// close everything
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
Log.d(LOG_TAG, "Update event");
EventBus.getDefault().post(new UpdateUIEvent());
}
});
}
}
How can I send event from a sync adapter to a fragment using Greenrobot's EventBus
?