Decoupled android app communication with EventBus

Rashi Karanpuria
Apr 12, 2017 · 2 min read

While creating an app which had a lot of in app communication in use, I used broadcast receivers with bundles and parcelables. It quickly grew into a lot of code which was tightly coupled and hard to keep track of. Not to mention the time spent removing error cases due to activity and fragment lifecycle events.
So I decided to find a better way out for this, I came across a post about loose coupled in app communication using EventBus and I decided to give it try.

EventBus is an open-source library for Android using the publisher/subscriber pattern for loose coupling.

The event bus :
• simplifies the communication between app components.
• decouples event senders and receivers.
• helps avoid complex and error-prone dependencies and life cycle issues in activities & fragments.
• can be used with services. Awesome!
• makes your code super simple.
• is a tiny library which is way faster.
• has advanced features like delivery threads, subscriber priorities, etc.

Wait, there is more:
• annotation based api.
• Main and background thread delivery.
• event and subscriber inheritance.
• configurable to your requirements.
• sticky events.

And after you are all impressed and ready to move to the first tutorial you can put your hands on, hold on it just takes 3 steps.

1. Define events:

public class LocationEvent {    Location location;    public LocationEvent(Location location) {
this.location = location;
}
public Location getLocation() {
return location;
}
}

2. Prepare subscribers: Declare and annotate your subscribing method, optionally specify a thread mode:

@Subscribe
public void onLocationEvent(LocationEvent event) {
/* Do what you need to */
};

Register and unregister your subscriber. For example in an Activity (depends on component lifecycle):

@Override
public void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}
@Override
public void onStop() {
super.onStop();
EventBus.getDefault().unregister(this);
}

You can use a singleton Eventbus too.

3. Post events:

EventBus.getDefault().post(new LocationEvent());

So here is a link to the EventBus’s github library : https://github.com/greenrobot/EventBus

Hope you are little excited about loose coupling and simpler in-app communication in Android. If you have any suggestion, question, or you would like to add something, just type it out in comments below!

Thanks for reading this article. Be sure to click ❤ below to recommend this article if you found it helpful.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store