What is EventBus ?

  • EventBus is a publish/subscribe event bus for Android and Java.

Why should you use EventBus ?

  • In any Android application, communication takes place between activities, fragments, services and other Android components. There are several options to communicate between such components. Some of them can be :
  • Interfaces
  • Broadcast Receivers
  • Intents
  • Asynctasks

Many a times, just to make this interaction possible, we forget about the complexity in the code. It becomes very difficult to test these if the code isn’t clean and concise. The more the dependencies among the components increase, the more difficult it becomes to manage the interaction between them.

By using the above mentioned options, we can face the following issues :

  • Code complexity
  • Code redundancy
  • Difficulty in testing
  • Lifecycle issues

So, there comes EventBus with a very simple solution. EventBus provides a way to interact between the components by using threading in a very effective manner.

How come?

Let’s see that.

Advantages of using EventBus

  • Very easy to use and understand
  • Simplifies code
  • Reduces errors
  • Helps to test code easily
  • Reduces the lines of code

How to integrate EventBus in Android?

Let’s start with adding the required dependency for using EventBus.

Add the following dependency in your application module’s gradle file :

compile ‘org.greenrobot:eventbus:3.1.0-RC’

In case you need to integrate via Maven, then add the following :

<dependency>
   <groupId>org.greenrobot</groupId>
   <artifactId>eventbus</artifactId>
   <version>3.1.0-RC</version>
</dependency>

Now, let’s take a quick look at the the steps to get started with EventBus :

Define an event

EventBus uses POJO to send the data. So, let’s define a simple POJO named     NotifyEvent:

public class NotifyEvent {

public String mNotifyMessage;

public NotifyEvent(String message) {

       mNotifyMessage = message;

}

}

Create Subscribers

Subscribers are the Android components which intend to listen to any number of event/s.

These components need to implement the following methods :

@Subscribe(threadMode = ThreadMode.MAIN)

   public void onNotifiedEvent(NotifyEvent notifyEvent) {

Toast.makeText(this, notifyEvent.mNotifyMessage, Toast.LENGTH_SHORT).show();

   }

You might be thinking the use of ThreadMode.MAIN. As of now, just keep in mind that it is a type of thread mode that works on UI thread. In order to know more about different types of thread modes, please refer EventBus thread modes.

Register/Unregister Subscribers

In order to be notified of the events, the subscribers need to register and unregister themselves from the bus. The subscriber will only be notified of the events if it has registered for the same. The subscriber needs to register in onStart() and unregister in onStop() as follows :

@Override

   public void onStart() {

       super.onStart();

       EventBus.getDefault().register(this);

}

@Override

public void onStop() {

   EventBus.getDefault().unregister(this);

   super.onStop();

}

Post Events

In order to post an event, use the post method as follows :

EventBus.getDefault().post(new NotifyEvent(“Hello world!”));

Types of Events

There are two types of events as follows :

Non Sticky Events

Non sticky events are usual events which are spontaneous i.e. if any subscriber related to that event is not found, then the event will be discarded. A non-sticky event can be posted as follows:

EventBus.getDefault().post(new NotifyEvent(“Non sticky event”));

Sticky Events

Sticky events, as the name suggests, stores the information which is supplied to it. So, in case you need to implement caching, you can make use of sticky events.

As for example, you need to fetch the location of the user. Now, in case location isn’t received due to any reason, we can get the last fetched location using a sticky event.

In order to post a sticky event, we need to call postSticky method as follows :

EventBus.getDefault().postSticky(new NotifyEvent(“Sticky event”));

After going through all of this, you might have realized how easy it is to use EventBus and thought of using it instantly if you haven’t done so !

Well, in case I have missed out anything, you can refer EventBus in Android for more details.

Suraj Makhija

Software Engineer(Android) at LetsNurture Infotech, a Passionate Android Explorer and a CR7 Fan

Want to work with us? We're hiring!