Local Broadcast vs EvenBus

from the CommonsWare Community archives

At April 29, 2018, 6:52am, Shahood asked:

Hello Mark,

If i understood correctly, local broadcast and eventbus are both used to communicate between activities or fragments. So, given a choice, which technique should we use? Im more comfortable with eventbus but is there something im missing if im not using local broadcast technique.
Would be great if u could give a comparison chart between the two.
I hope it doesnt amount to asking for too much!

Thanks
Shahood


At April 29, 2018, 11:01am, mmurphy replied:

which technique should we use?

Whichever you want. They fill the same role.

is there something im missing if im not using local broadcast technique

IMHO, no.


At April 29, 2018, 3:22pm, Shahood replied:

Thanks for the prompt reply!


At April 29, 2018, 3:34pm, Shahood replied:

Oh yeah, and what if i plan to communicate through an interface, kind of like that Contract technique? Would it be the same thing?


At April 29, 2018, 3:46pm, mmurphy replied:

I do not know what you mean, sorry. I am not aware of any event bus implementation that has anything to do with Java interfaces. The Contract pattern has little to do with event buses.


At April 29, 2018, 4:32pm, Shahood replied:

I’m just trying to have a comparison of different techniques that let u have a communication link between activities/fragments.
Contract pattern as far as I remember is also based on Java interface. So if I want to communicate, let’s say, the result of a method from a fragment to the activity, do I have a choice among LocalBroadCast, EventBus and Contract pattern? If yes, how would I prefer one on the other?


At April 29, 2018, 4:46pm, mmurphy replied:

So if I want to communicate, let’s say, the result of a method from a fragment to the activity, do I have a choice among LocalBroadCast, EventBus and Contract pattern?

Technically, yes, among an infinite number of possibilities.

If yes, how would I prefer one on the other?

Use the contract pattern. Event buses, at most, should be used for communications between components (activities, services, receivers), cases where there are no direct connections. In cases where you have a direct connection — such as a fragment having a reference to its hosting activity — use that.

In “The Busy Coder’s Guide to Android Development”, I sometimes use an event bus for communication from a background thread to the main application thread. That’s not great, and I am slowly changing those samples to use other techniques (e.g., RxJava).


At April 29, 2018, 5:35pm, Shahood replied:

So, for cases where there is a direct connection, we should use Contract pattern. In case of no direct connection, we should use EventBus.
Fair enough!

And where should we prefer using a LocalBroadcast?


At April 29, 2018, 5:49pm, mmurphy replied:

I refer to “event bus” (two words) as a generic term. Both greenrobot’s EventBus (one word) and LocalBroadcastManager implement the event bus pattern. There are other event bus implementations as well. Whether you use greenrobot’s EventBus, LocalBroadcastManager, or another event bus implementation, for your event bus scenarios, is up to you.


At April 30, 2018, 12:38am, Shahood replied:

Oh ok! Thanks for the detailed reply!