28

I have many Activities which raise background tasks; the Activities will pass themselves in as having implemented a listener callback, so that the background tasks can raise an event on the Activities. The Activities in turn can show something on the UI to indicate that a background activity passed or failed.

Alternatively, I could use an EventBus, wherein I get the Activity to register itself as a listener/subscriber. I can have a background tasks raise an event on the EventBus and the Activity listening to it can handle it.

What are the advantages of one over the other? When would you use one over the other? (Code cleanliness? Performance? Caveats?)


Follow up - I did end up using EventBus. The code is definitely a lot cleaner and there aren't callbacks hanging out everywhere. The IDE (IntelliJ) thinks that the onEvent methods are unused, so I created an annotation

@Target({ElementType.METHOD})
public @interface EventBusHook {}

and placed it over my onEvent methods. Then Alt+Clicked on it and asked IntelliJ to not treat it as unused.

@EventBusHook
public void onEvent(MyEventType myEventType){
1
  • i would use a local bound service where tasks are launched and an activity thats binds to that service and creates a "bound" to send the results
    – pskink
    Commented Mar 12, 2015 at 11:27

5 Answers 5

55

I disagree with @nnuneoi's answer.

Event bus has just one single advantage: it allows for communication between components which are "unaware" of each other's existence.

And there are several disadvantages:

  1. The components become loosely coupled by dependency on both event bus and specific event type
  2. The coupling described in #1 above is not strong
  3. The coupling described in #1 above is not evident
  4. Event bus introduces performance overhead over simple callbacks
  5. If event bus holds a strong reference to subscribers (as is the case with e.g. GreenRobot's EventBus), then unregistered subscribers will cause memory leaks

Given all these disadvantages, simple callbacks should be the default implementation choice.

Event bus should be used only when direct coupling is not desired or hard to implement. For example:

  1. Sending events from Service to Activity
  2. Exchanging events between independent Fragments
  3. Application wide events (e.g. user login/logout)

If the communicating components are already "aware" of each other's existence, there is no need for them communicating via event bus.

4
  • The argument that components will be loosely coupled is reason enough to not use EventBus for simple Activity / Fragment communications. Since a Fragment is attached to the Activity anyways. Commented Oct 13, 2017 at 9:11
  • 2
    @MuratK., not exactly. Fragment is attached to Activity, but in order to be able to communicate from a specific Fragment to a specific Activity, the fragment will need to be coupled to that specific Activity class (or some interface that the Activity implements). This is a stronger type of coupling that also requires casting of the reference returned from getActivity() call. Therefore, in some cases, it is cleaner to just use EvenBus. In case of Fragment to Fragment communication (through Activity) the coupling will be even tighter, which makes EventBus even more beneficial.
    – Vasiliy
    Commented Oct 13, 2017 at 12:59
  • @Vasiliy I have a Fragment and Background Service. Service - data source, Fragment - subscriber (displays data). I use the Green EventBus. In the event that the Activity is killed, the Service loses its subscriber, but continues to work. In this case, can there be memory leaks?
    – tim4dev
    Commented Oct 14, 2017 at 21:54
  • 2
    @tim4dev, as long as the Activities and Fragments that registered to EventBus are unregistered in their onStop() methods - there will be no memory leaks. The Service might continue posting events even when there is no subscribers. BTW, in situations like yours I find sticky events to be quite useful.
    – Vasiliy
    Commented Oct 24, 2017 at 10:20
26

Benefits of using EventBus:

  • Your code will look much more clean
  • Your code will become more modular which will allow you to easily create test case for your code
  • Avoid memory leaks from bad object references which lock the object and does not allow Garbage Collector to clean it up
  • Could have more than one receiver a time, that it much like broadcasting
  • Simplify multiple interfaces into a single one, EventBus
  • In an interface class, you need to override every single method in the class that is inherited. With EventBus, you can listen for just an event that you really want

But bad part is you might be a little bit more headache with the function declaration since IDE couldn't help you with auto-complete.

My suggestion is, if you find that you have to create a custom listener, then please consider EventBus, it might be a better choice for most of (if not all) of your requirements/cases.

Anyway, it is all your choice after all =)

1
  • 1
    Thanks, I have been leaning towards EventBus because of your first 3 points. I'll try it out and see how it fares in IntelliJ.
    – Mendhak
    Commented Mar 12, 2015 at 19:01
1

You should check if your event is globally unique on the semantic view. Either an subscriber is interested in the event or not. If not he shouldn't subscribe.

Event-Bus mechanism is right if you really have a publisher-subscriber relationship. The event must be totally independent of the receiver.

So a subscriber that discard the event for any reason of responsibility ("I am not responsible the event even if I am registered") is a strong indicator that using an Event-Bus is wrong. Then you should consider using dedicated listeners.

1

I would go with the EventBus because of the loose coupling and cleaner code. Also, the fact that using an EventBus such as Greenrobot automatically does all the boilerplate for me and allows me to register & deregister observers right from Activity Lifecycle methods (onStart and onDestroy|onStop) is great. Implementing callbacks and still managing to control Activity lifecycle management for those callbacks is an unnecessary headache and involves a lot of unnecessary boilerplate.

Also, apparently all garbage collectors think weak reference is great-Event bus gives your observers and components exactly that. Its the basis of the Observer pattern.

0

In a single sentence, event bus is a classic God Object and is therefore considered an anti-pattern and should be avoided.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.