EventBus is the number 1 event library for android and java. EventBus uses the publisher and subscriber pattern for loose coupling which means the possibility of errors will be less. EventBus simplifies the communication between different components for example communications between different levels of activities or even services. You need very less or no setup to use EventBus in your existing project.
EventBus provides a convenient annotation-based API which makes it fast and the performance is increased. EventBus is used by many popular apps, apps which has 1 billion-plus download which shows how EventBus popular.
In this tutorial, we will see how we can get started using EventBus in an android project. Before jumping in the code let's see what we will be going to make. To better understanding EventBus we will make an app that allows us to add items to the cart and show the total count of the items. See below how our app will work.
Let's get started.
Add EventBus Dependency in Android Studio
First, we need to add EventBus Dependency in the project. Open your build.gradle and add the dependency.
Hit the sync button and move to the next step.
Make a POJO class for EventBus
The next step is to make a Java object class to pass in the EevntBus Subscribe method and to be used later.
For our example app, we will make a class with the name of CartEvent.java with a string field for cart items.
See the below code for better understanding.
Move to the next step.
Make UI for the App
In the first activity, we have a textview to show the total count of the cart item.
And a button to open second activity to add items in the cart.
See the below code to understand.
Now make the second activity to show the items list and add a method for the button click.
We made simple cart items. We add 3 items but in the real world application, you should use recyclerview or any other view to make a list of items.
Here is the second activity XML code.
In the above code, we have added the onClick attribute and pass the addItemToCart method name.
And made the appropriate method in the second activity java.
Now implement EventBus in the project.
Let's make the subscriber and in the subscriber method, we will add items to the list.
See below to understand.
Make Subscribe Method of EventBus to update Cart
We will show cart count in the first activity so we need to update cart count in the subscriber method. Open your java file and create a method onCartItemAdd and in the parameter, we will get our CartEvent object.
We will add this object to the list and then show the list size in the cart count.
To tell the EventBus to trigger this method we need to add the @Subscribe annotation to the method.
We should unregister and re-register the EventBus in the onStart and onDestroy method on the activity.
Now we will call this method on the add item click of second activity.
In the second activity, we already created a button onClick method. In that method, we will call the EventBus post method and pass our CartEvent object.
That's it. Now run your app and add an item to the cart and back to the first activity then you can see your updated cart item count.
This is just a simple example to get started in EventBus, you can use EventBus wherever it fits in your requirement.
So guys this is the end of this article, I hope this article helped you somewhere.
Don't forget to support us and share this article with your friends or batchmates.
Thank you and have a nice day.
Subscribe to our Newsletter
By subscribing to our newsletter you're agreeing with our T&C.