Search code examples
androideventsbusotto

Is an Event Bus (Otto or Greenrobot) dependant on the onCreate*()-callbacks?


I have a specific question concerning the event bus design pattern in combination with androids activities/fragments/services.

What I understood so far: When using an event bus I create, lets say, a single event bus for my whole application (singleton design pattern). Now I can make single activities/fragments/services post an event on the event bus and the event bus spreads the news to all other activities/fragments/services which subscribed to this specific event. This way all other activities/fragments/services can react to this specific event. In order for all activities/fragments/services to be able to use the event bus they have to register with the bus (prefereably in their onCreate*()-methods) and they should unregister if they are about to be stopped/canceled/destroyed.

This brings me to my question: Lets say I want to implement a brand new actionbar (NOT the android one or sherlock or any other, I mean from scratch here). As far as my understanding goes I could implement an actionbar with an event bus. So I would have my main activity with buttons on top of the activity-layout and each button has its own onClick-method implemented. For each button I would have a fragment that becomes visible once the associated button is clicked. Now within each onClick-method (one for each button) I could post an event to the event bus and the bus spreads it to the fragments. The fragments then check whether they should go visible or not depending on the additional event information.

Now the problem here is, each fragment would need to register with the event bus first, so in order for this to work each fragments onCreate()-method must be called at least once, is that right?

Thank you for any insights into this!

Steffen


Solution

  • In order for all activities/fragments/services to be able to use the event bus they have to register with the bus (prefereably in their onCreate*()-methods)

    onCreate() may or may not be the proper spot, particularly for activities and fragments.

    and they should unregister if they are about to be stopped/canceled/destroyed.

    Which is why onCreate() may or may not be the proper spot for registration. Registration and unregistration should occur in paired lifecycle methods.

    Lets say I want to implement a brand new actionbar (NOT the android one or sherlock or any other, I mean from scratch here)

    Off the cuff, note that nothing in your description would require this, as it all can be accomplished with the standard action bar or its backports.

    Now within each onClick-method (one for each button) I could post an event to the event bus and the bus spreads it to the fragments. The fragments then check whether they should go visible or not depending on the additional event information.

    Or, the activity could just tell the fragment "yo, your button was clicked". Event buses are great for cases where there is no direct connection from the sender to the recipient (e.g., service to activity), or where the event may be of interest to several parties. In this case, using an event bus would seem to be unnecessary, since the activity already knows its fragments.

    each fragment would need to register with the event bus first, so in order for this to work each fragments onCreate()-method must be called at least once, is that right?

    Correct.