Local Broadcast vs EvenBus


#1

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


#2

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.


#3

Thanks for the prompt reply!


#4

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


#5

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.


#6

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?


#7

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).


#8

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?


#9

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.


#10

Oh ok! Thanks for the detailed reply!