How To Use EventBus – A Beautiful Android Library

EventBus is an Android optimized event bus that simplifies communication between Activities, Fragments, Threads, Services, etc. Less code, better quality. You see, if you have developed android applications before, you will agree with me when I say that communication between different parts of the project code can be a pain. Using interfaces might work but not all the times. The idea though would be to stick to the best design patterns in Java which include decoupling your code. Eliminate dependencies. Let us get started!


First Things First

To use EventBus, you should add the following to your build.gradle file (app-level file)

Sync your project! Now, moving on ….

Next, create a simple Java class that will represent your event.

Now is the time to use EventBus. It is easier than you might actually think.

Use Case

In this typical scenario, a user attempts a LOGIN by making an HTTP request to a remote server somewhere in the North Pole or your basement! We both know that in such a network operation, a user will have to wait for the request to complete – this could take a few seconds depending on the speed of the internet connection.

What you might want to do is listen for login success or login failure and then respond accordingly to redirect the user to the respective part of the application. Now let us do this.

Before we come back to handle notifications from our publisher inside our activity, let us write up our class that acts as our PUBLISHER.

I know, don’t call me names for using an AsyncTask for this example. Let us talk about why they are not good for your health later on! This is for demonstration purposes ONLY.

Pretty much done here …

Finally, we need to handle the events we just posted above from within our subscribing activity:

To do that, we have to add a single method whose parameter is our Event class – the one we created earlier!

Ladies and gentlemen, that is how you can easily communicate between different parts of your application without having to write a lot of code or re-inventing the wheel with your own Subscriber or Publisher code!

Like I said earlier, it decouples your code (aka loose coupling).

Although this approach of using EventBus might sound like a work of beauty, it won’t be long before you realize that too many events will result in everyone posting events in your application – this will be recipe for disaster since you might not know who is talking to who and when!

So, use it but not in excess 🙂

I hope you found this post helpful to you and if that is true, please share it with your friends online and consider subscribing for future posts!

Thanks and happy coding my friends!

Be The First To Get New Posts From Simple Developer…

Written By Elisha Chirchir

Elisha Chirchir is a software developer. He is also the founder of Simple Developer and co-founder of Instinctive Software Solutions. On any given day, he works on both Android and Web Development. During his 'free time', he offers training to those interested in learning how to code in php, java, python, javaScript etc. You can easily find him on StackOverflow Android chatroom or on Twitter @Eenvincible

Leave a Reply

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