Did you ever heard about Publish-Subscribe pattern in Software architecture?

Yes. It’s a messaging pattern where the senders of the message, called the publishers will never know who are going to receive those messages  and the receivers, also known as the subscribers, won’t know who sent those messages as well.

The publishers will just send the message, without knowing who is gonna receive it. They do their work right.

On the other hand, the receivers, I mean the subscribers, will subscribe what kind of message they want to get. Doesn’t matter who’ll send them. Well, why should they care? They’re gonna get what they need, right? 😀

Okay, let’s change the perspective a little. Think of an event as a message here. Now think about the publish-subscriber pattern. Yes, you got it right!

There will be event publishers instead of message publishers. And the subscribers will receive events instead of messages. So that whenever an event is published (of course by the publishers), the subscribers can receive that event and execute their code.

Here comes the awesomeness of the EventBus dependency for Android. It enables you to use publish-subscribe software architecture pattern. I’m using this library for several days and I’m just amazed. I don’t have to worry about event handlers now, I don’t need fragment interaction listeners, my code is clean and organised.

Okay let’s see how can we use this api in real life project. There’s a getting started page where you can find the instructions to use it. I recommend you to take a look on that page.

I’m creating a real world project so that we can get a hands on experience before we can use it.

First thing first, Let’s create an Android Studio project. I’m naming my project as EventBusDemo.

Add EventBus dependency on our app level build.gradle file:

We’ll have a textview and a button on our activity and a fragment (It’ll contain a textview and a button) so that we can illustrate the interaction between activity and fragment with eventbus as well.

Our goal is, whenever a button is clicked, all of the textviews (regardless activity textview or fragment textview) will be updated. So we have to post an even when a button is clicked, right? Then all of the methods that subscribes that event will update the textviews. or do whatever  they meant to do.

Let’s create our event first. It’s a plain old Java object(pojo). We name it MessageEvent





We’ve registered eventbus on onCreate(). Best practice is to do it on onStart(). But it’s just a demo.

We have a method onMessageEvent() that subscribes MessageEvent, taking a MessageEvent object as a argument. That means whenever a message event will be posted this method will be executed. We’ve updated the textview with the message from message event.

We’ve implemented another subscriber method to update fragment textview in MainFragment.java.

So these two subscriber method will be executed without knowing who is the publishers or what’s inside this event. It’s just waiting for the MessageEvent to be published.


We’ve posted event with custom message on onClickListeners of both activityButton and fragmentButton. When someone click those buttons, an event will be posted with a message. and those methods that subscribes that event will be executed, in this case, the textviews will be updated. And that’s how it works!

Leave a Comment