Android EventBus Publish Subscribe Tutorial

What is event bus? Eventbus is an android library that enables the developer easier to communicate between the components such as activities, fragments, services and more. It is very simple to use, less code and produce better quality in the Android application. Eventbus is very popular to use in the worldwide and this library creates by the famous GreenRobot. In this tutorial, I will tell you how to use android Eventbus publish subscribe the event on your android project.

Creating a New Project

1. Open Android Studio IDE in your computer.
2. Create a new project and Edit the Application name to “EventBusExample”.
(Optional) You can edit the company domain or select the suitable location for current project tutorial. Then click next button to proceed.
3. Select Minimum SDK (API 15:Android 4.0.3 (IceCreamSandwich). I choose the API 15 because many android devices currently are support more than API 15. Click Next button.
4. Choose “Empty Activity” and Click Next button
5. Lastly, press finish button.

Add eventbus dependency

Go to your build.gradle (Module:app) and add one dependency in the dependencies section.

Create a Modal

Right-click your java package name and create a new modal class “User”. After that add three attributes in this class which are the name, phone, and email.

Create a new empty activity

Right-click your project and create an empty activity for “SecondActivity” in your project. After that edit your activity_second.xml to the following source

Edit class

Open your class and follow the example source in the below. This activity subscribes the event from the main activity.

Edit mainactivity.xml layout

This XML file will add some edited text to publish the event in the main activity.


Go to the class and edit the following source code. This activity registers the event bus in the onstart method. PostSticky means it will store for the future use until the app is destroyed while Post only able to receive the class.

Run Your Project

At last, you can run this project in your android studio and test it out in the emulator or real android device to check out the features.

(Android EventBus Publish Subscribe)

Source Code

(Visited 404 times, 1 visits today)

Yong Loon Ng

Ng Yong Loon, better known as Kristofer is a software engineer and computer scientist who doubles up as an entrepreneur.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *