Android EventBus Library Example

In this android tutorial, we are going to explore android EventBus. EventBus is an open-source library for Android using the publisher/subscriber pattern for loose coupling. EventBus enables central communication to decoupled classes with just a few lines of code – simplifying the code, removing dependencies, and speeding up app development.

If you have work with android components you will realized that there are many way you can use to communicate between different components.

When the communication is between two different Activity classes we usually use Intent class. For communication between Activity and a child Fragment, there are few options available to use. We can store the message in a bundle or we can create an interface to is connect to the parent Activity or by creating a set method.

When it involves communicating between Activity and a Service class, it is a different ball game. We might choose to use a Broadcast Receiver or inter-process communication for bound Service.

The list goes on. So there is no simple and single way to achieve this kind of communication in a decouple way in android. This is where Android EventBus goes to our need.

Android EventBus Library was created by a company name Greenrobot. You can read more about android EventBus in their developer’s use guide.

To understand the benefits that EventBus brings to the table when developing android application, we have listed it below

  1. Simplifies the communication between components
  2. Decouples event senders and receivers
  3. Performs well with Activities, Fragments, and background threads
  4. Avoids complex and error-prone dependencies and life cycle issues
  5. Is fast; specifically optimized for high performance
  6. Is tiny (<50k jar)
  7. Is proven in practice by apps with 100,000,000+ installs
  8. Has advanced features like delivery threads, subscriber priorities, etc.

Android EventBus library works like a Publisher / Subscriber design pattern where by the publisher emits events and the subscribers consumes the events. The subscribers take action based on the emitted event.

The Publishers uses the EventBus class static method getDefault() to instance a single copy of the EventBus in the application. Although you can create multiple instance if you want to.

The Event Message Object is a plain Java POJO which contains the message intended to be passed through the event bus from the Publisher to Subscribers. Multiple subscribers can subscribe to a single publisher. The subscribers method are denoted with @Subscriber annotation. The method also accepts an instance of the event message object as parameter.

In this exercise, we are going to send an event message from a ServiceIntent to Activity class when a user clicks a button in the activity.

The message received from the service will be displayed using a TextView widget in the Activity class.

With the information we had so far, we will proceed to create an android application that will show case how to use android EventBus library.

In other to get a visual understanding of what we are going to create in this android tutorial, I have add below some screen shots from the application.


android eventbus


Lets start to soil our hands in code. Start up your IDE. For this tutorial, I am using the following tools and environment, feel free to use what works for you.

Windows 10

Android Studio

Sony Xperia ZL

Min SDK 14

Target SDK 25

To create a new android application project, follow the steps as stipulated below.

Go to File menu

Click on New menu

Click on Android Application

Enter Project name: AndroidEventBus

Package: com.inducesmile.androideventbus

Select Empty Activity

Name your activity: MainActivity

Keep other default selections

Continue to click on next button until Finish button is active, then click on Finish Button.

Add Library Dependencies in Build.gradle

Open the module build.gradle, since we are going to use EventBus library, we are going to add the library to the dependencies section. This will make it possible for our android project to access all the classes and method that are exposed in this library.

Once you open build.gradle file, add the code below to the file.

apply plugin: ''
apply plugin: ''
android {
    compileSdkVersion 25
    buildToolsVersion "24.0.1"
    defaultConfig {
        applicationId "com.inducesmile.androideventbus"
        minSdkVersion 14
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner ""
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), ''
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('', {
        exclude group: '', module: 'support-annotations'
    compile ''
    compile 'org.greenrobot:eventbus:3.0.0'
    testCompile 'junit:junit:4.12'


We are going to update our project strings.xml file located in the values folder inside the res folder. Open the file and add the code below to it.

    <string name="app_name">Android EventBus</string>
    <string name="event_message">This simple app gets a message from Event bus on a Service and display the message on a TextView widget</string>
    <string name="start_intent_service">START INTENT SERVICE</string>
    <string name="event_message_from_service">Message from EventBus in Service Class</string>


Open the colors.xml file in the same location as the strings.xml file and add the code below to the file.

<?xml version="1.0" encoding="utf-8"?>
    <color name="colorPrimary">#7b8594</color>
    <color name="colorPrimaryDark">#5b636f</color>
    <color name="colorAccent">#7E6CFA</color>
    <color name="colorLight">#98A9B9</color>
    <color name="colorWhite">#ffffff</color>
    <color name="colorBlack">#000000</color>


Let’s create the MainActivity class layout file. As stated above, this layout file will contain two TextView, and a Button widgets. When the Button is clicked, it starts a background Service which will post message back to the Activity class and this message is displayed in one of the TextView widgets.

Open this layout file and add the code below.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=""
        android:layout_alignParentTop="true" />
        android:textColor="@color/colorAccent" />

MainActivity Class

Open the MainActivity class, in this class we will get the instances of our TextView and Button widgets.

The button object is wired with a click listener event. Inside the onClick() method of the Listener class, an IntentService is started.

If you do know much about android Service class, you can read it up in android developers documentation.

IntentService runs on a different thread. So to update a UI view from another thread we will use the EventBus to achieve this.

A messageEventFromService() method which is annotate with @Subscribe is a subscriber to the event publisher.

Inside the method, the event message passed is obtained and assigns as a value to TextView.

Open MainActivity class and add the code below.

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
public class MainActivity extends AppCompatActivity {
    private static final String TAG = MainActivity.class.getSimpleName();
    private TextView displayTextView;
    protected void onCreate(Bundle savedInstanceState) {
        displayTextView = (TextView)findViewById(;
        Button serviceButton = (Button)findViewById(;
        serviceButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent serviceIntent = new Intent(MainActivity.this, MyService.class);
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void messageEventFromService(EventMessage event){
    protected void onStart() {
    protected void onStop() {

MyService Class

In the default project package, create a new java class and name it This class will extends IntentService class and override the onHandleIntent(); This is where the intent the component that started it is obtained.

The default constructor of the service is also declare. In the onHandleIntent() method, an instance of the EventBus is obtained by getDefault() method of the EventBus. The post() method is call with the Event Message object passed as parameter.

Open this class and add the code below.

import android.content.Intent;
import org.greenrobot.eventbus.EventBus;
public class MyService extends IntentService {
    public MyService() {
        super("MY SERVICE");
    protected void onHandleIntent(Intent intent) {
        EventBus.getDefault().post(new EventMessage(getString(R.string.event_message_from_service)));

EventMessage Class

Create a new java class file like above but now name it EventMessage,java. This is a plain POJO java class.

Open the class and paste the code below.

public class EventMessage {
    private String notification;
    public EventMessage(String notification) {
        this.notification = notification;
    public String getNotification() {
        return notification;

This brings us to the end of this tutorial. I hope that you have learn something. Run your app and see for yourself.

You can download the code for this tutorial above. If you are having hard time downloading the tutorial, kindly contact me.

Remember to subscribe with your email address to be among the first to receive my new android blog post once it is published.


  1. Avatar
  2. Inducesmile

Add a Comment