Building an Android application that has various dynamic segments speaking with one another can get repetitive. To save time, developers often end up with tightly coupled components in their apps like Interfaces or directly calling an object’s function.
EventBus is a popular open-source library that was created to solve this problem using the publisher/subscriber pattern.
Using the EventBus library, you can pass messages from one class to one or more classes in just a few lines of code.

Source - GreenRobot library
Methods of EventBus
- Register - To register your component to subscribe to events on the bus.
- Unregister - To stop receiving events of the bus.
- Post - To publish your event on the bus.
Add EventBus to your project,
- Compile 'de.greenrobot:eventbus:3.0.0'
- EventBus bus = EventBus.getDefault();
- bus.register(this);
Create an Event,
- EventBus bus=EventBus.getDefault();
- bus.post("Pass Message here.");
Registering your component will be the same, the only change you need to do is in your onEvent() and pass your object while creating event.
- public class PersonClass
- {
- String name;
- public PersonClass(String message)
- {
- this.name = name;
- }
- public String getName()
- {
- return name;
- }
- public void setName(String name)
- {
- this.name = name;
- }
- }
- change parameter in onEvent()
- @Subscribe
- public void onEvent(PersonClass person)
- {
- Log.i("Name of person is : ", person.getName());
- }
- EventBus bus=EventBus.getDefault();
- bus.post(new PersonClass("Ravi Rupareliya"));

Join the conversation! Your thoughts help the community grow.