4

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);
}
Jaythaking
  • 2,200
  • 4
  • 25
  • 67
  • 1
    You might want to have a look at sticky events – Leo supports Monica Cellio Sep 15 '16 at 22:50
  • Do I have to handle those Event manually or is it delivered automatically once I register back? How should I manage that in my situation? – Jaythaking Sep 15 '16 at 22:54
  • You do not need to be registered on the bus to post messages on the bus. Are you using an event bus for routing messages within a single activity? And, if yes, why? – CommonsWare Sep 15 '16 at 22:56
  • No, I have a dispatcher which is linked to the bus and post the event to another class – Jaythaking Sep 15 '16 at 23:03
  • I update my question to be more faithful to my code... – Jaythaking Sep 15 '16 at 23:09
  • Try to register onResume and unregister in onStop. – android_eng Sep 15 '16 at 23:17
  • Didn't work.... – Jaythaking Sep 15 '16 at 23:28
  • You should register onCreate, the moment activity is ready. Bus doesn't have to wait for the view. – user3623735 Sep 15 '16 at 23:30
  • Why would they tell otherwise on their doc ? :/ – Jaythaking Sep 15 '16 at 23:40
  • @NannuoLei it's finally your answer that did it... I adapted my dispatcher to handle sticky event and now it's working... thank you – Jaythaking Sep 15 '16 at 23:46
  • Hi @Jaythaking, glad to know! I was in class so no way to reply further, sorry about that! – Leo supports Monica Cellio Sep 16 '16 at 04:25
  • @LeonardoAcevedo If you make a well explained answer, I'll accept it... – Jaythaking Sep 26 '16 at 19:39
  • @jaythaking thanks for the offer. However, after re-reading your question again, it seems to me that sticky events are actually not that useful here. Since your tell your dispatcher who should receive your event, chances are that it's the dispatcher who's complaining about the bus not being registered. I don't have your dispatcher's code so I don't know which modifications you introduced in it that used sticky events, but I think chances are that if you make that code not sticky again, it should work just fine. – Leo supports Monica Cellio Sep 26 '16 at 21:53
  • The dispatcher was actually sending event when activity weren't ready (onPaused()) ... I didn't post the whole architecture code as nobody would have answered me. If I remove sticky event, it's breaking again so your clue does the trick for my case :p – Jaythaking Sep 26 '16 at 21:58

1 Answers1

1

This is the same problem as why you're getting IllegalStateException if you're trying to show a dialog fragment in onActivityResult. Simply, it runs before the UI is back to life.

Simple solutions:

1.)

Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
    @Override
    public void run() {
         actionsCreator.uploadFile(filepath, "image/jpg");
    }
});

2.) while the bus is paused (this is something you'd handle manually), you should queue up the events, and then when it's unpaused, execute them.

Community
  • 1
  • 1
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
  • But I dont get why an event bus have the same lifecycle as a view? Why wouldn't I be able to communicate with some other service when the view isn't ready yet? – Jaythaking Sep 15 '16 at 23:23