Codementor Events

How to use the EventBus library

Published Jun 20, 2017

Greenrobot's EventBus is a tiny library that allows publish-subscribe style communication between components without requiring the components to explicitly register with one another.

That makes the data exchange between components like Activity, Fragment, Services and any kind of backgrounds threads pretty easy.

Why should I care about Event Bus?

The short answer: loose coupling.

The long answer: Normally, a software is divided into multiple layers like presentation layer (Activity, Fragment), business layer (e.g. UserService) and data layer (e.g. UserDao). Besides the technical layers, there are also functional layers.

Sometimes you want to process specific events that are interested for multiple parts of your application. Event Bus offers a solution for this case: It provides a centralized way to notify software components that are interested in a specific type of event. There exists no direct coupling between the code of the publisher of the event and the code that receives it. A well designed decoupled system will reduce the need to make changes across components in case anything changes.

An Event Bus can also massively reduce boilerplate code: No need for interfaces, callbacks for asynchronous communication or data propagation through all software layers. It keeps your software architecture decoupled and flexible.

Basics

The first step is to add the greenrobot EventBus dependency to your app's build.gradle file. Don't worry, the library is tiny (< 50 kB):

compile 'de.greenrobot:eventbus:2.4.0'

You need the following four parts to send and receive messages through your EventBus:

The bus
The event
The sender
The subscriber

The bus

For most applications, you will only need to use one EventBus object throughout your app. There is no need for explicit object instantiation. Just call anywhere in your code EventBus.getDefault() to receive the „default” event bus instance:

EventBus myEventBus = EventBus.getDefault();

The event

Events are just POJO (Plain Old Java Object) without any specific implementation:

public class HelloWorldEvent {
private final String message;

public HelloWorldEvent(String message) {
    this.message = message;
}

public String getMessage() {
    return message;
}

}

An event is just an object that is posted from the sender on the bus and will be delivered to any receiver class subscribing to the same event type. That's it!

The sender

You can post any event from any part of your whole Android application. Just get the default EventBus instance by calling EventBus.getDefault() and submit any event by calling post():

EventBus.getDefault().post(new HelloWorldEvent("Hello EventBus!”);

The subscriber

Ok, you have a bus, an event and already a sender. What's missing? Right, someone should listen to our HelloWorldEvent. You can declare as many subscribers as you want and you are not limited to specific Android classes. Theoretically, every Java class can act as a subscriber.

Registration:

You only have to call

EventBus.getDefault().register(this); // this == your class instance

to listen and subscribe to events.

If you want to use an Activity or Fragment as a subscriber, just call the statement above inside onStart(), onCreate() or onResume().

Unregistration:

You should also unregister the subscriber. Just call unregister(this) if you do not longer need the Event Bus:

EventBus.getDefault().unregister(this);

Listen to events:

You have to declare a method inside your subscriber class with the name onEvent for every type of event you want to subscribe:

// This method will be called when a HelloWorldEvent is posted
public void onEvent(HelloWorldEvent event){
// your implementation
Toast.makeText(getActivity(), event.getMessage(), Toast.LENGTH_SHORT).show();
}

No annotations and no configuration are necessary! You also don't have to implement a specific interface. Just plain old and easily understandable code. That is a prime example for the software design paradigm convention over configuration.

The convention:

method name: onEvent
parameter: only one method parameter
visibility: public
return type: void

Of course, a subscriber can also listen to multiple events at the same time. The following snippet shows the necessary code to prepare a Fragment to listen to two different types of event:
@Override
public void onResume() {
super.onResume();
EventBus.getDefault().register(this);
}

@Override
public void onPause() {
EventBus.getDefault().unregister(this);
super.onPause();
}

// This method will be called when a HelloWorldEvent is posted
public void onEvent(HelloWorldEvent event){
Toast.makeText(getActivity(), event.getMessage(), Toast.LENGTH_SHORT).show();
}

// This method will be called when a JustAnotherEvent is posted
public void onEvent(JustAnotherEvent event){
processEvent(event);
}

thanks for reading. For detailed info click

Discover and read more posts from Prem Gupta
get started
post commentsBe the first to share your opinion
Show more replies