I've got this problem since I switched to EventBus (same would occurs with any bus library) where whenever I want to perform an action when the view isn't ready, then I will get the error that the bus isn't registered;
E/EventBus: Could not dispatch event: class com.android.greenfield.Action to subscribing class class com.android.greenfield.GreenStore
It occurs when I want to fire an Action in those parts of the lifecycle:
When I take a Picture/Video:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
actionsCreator.uploadFile(filepath, "image/jpg");
// ... (Error here because the bus isn't yet registered)
}
Or here when I receive an NFC TAG:
@Override
public void onNewIntent(Intent intent) {
actionsCreator.uploadNfcTag(intent);
// ... (Error here because the bus isn't yet registered)
}
If I follow the normal way or EventBus as they say in their doc, I should register
and unregister
this way:
@Override
public void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}
@Override
public void onStop() {
EventBus.getDefault().unregister(this);
super.onStop();
}
The only workaround I found so far is registering and unregistering when I want to perform an action that is in between the onStart()
and onStop()
lifecycle... But it's dirty and I feel bad
@Override
public void onNewIntent(Intent intent) {
dispatcher.register(GreenStore);
actionsCreator.uplaodNfcTag(intent);
dispatcher.register(GreenStore);
}