Now if you have been developing Android apps the most common logical problems you face are how to share data objects among different Activities or maybe more complicated situations like sharing between Activity and Fragment or maybe even more complex where there is no Context of any Activity or Fragment.
Though by now you must be thinking of Context passing or static methods, but then let’s be honest they are really painfull to handle. So what’s a better alternative?
Aha! The EventBus!
Before we start learning it, here is the gradle dependency:
compile 'org.greenrobot:eventbus:3.0.0'
EventBus is an android library by GreenRobot. Let’s understand how it will really help. There are basically three things you should know to use it.
The POST call
The POST method fires an Event with the required object data that you want to share where ever you need. Let’s see the code for it:
EventBus.getDefault().post(new CustomEvent(eventItem));
What? Just one line and all the worries are gone? YES!
So can I call this from anywhere? Yes, as you can notice getDefault() is a static method and the call does not need any other parameter other than a CustomEvent class and a eventItem(we’ll learn in the next step about them). So no more Contexts or object passing. Yay!yThe CustomEvent.class
The CustomEvent.class
So as we had a look at how to call POST method, we came across CustomEvent. This is a custom class in which we pass our required objects and it creates it’s own object packing all the objects into one to pass further. Let’s create one!
public class CustomEvent { public CustomItem eventItem;
public CustomEvent(CustomItem eventItem) { this.eventItem = eventItem; } }
So we made a new class CustomEvent with CustomItem object and constructor to pass the values.
Note: You can create as many objects in the class as per the requirement.
The @Subscribe
So till now we created a POST method to pass the values and CustomEventclass to contain all our object values, but now we need to retreive the passed values when the POST call is fired. Let’s see the code how to do it:
@Subscribe public void OnCustomEvent(CustomEvent event) { CustomItem eventItem = event.eventItem; }
And we have our CustomEvent class object and we can get values easily.
The @Subscribe annotation here specifies that the below function will execute taking single parameter of type CustomEvent. Where ever this is placed along with the CustomEvent type parameter, this will run as soon as the POST method is fired.
Note: Your parameter must have same parameter type as fired with POST.
Now we also need to register EventBus to make the current activity or fragment able to recieve the event message.
@Override public void onStart() { super.onStart(); if (!EventBus.getDefault().isRegistered(this)) EventBus.getDefault().register(this); }
@Override public void onDestroy() { super.onDestroy(); EventBus.getDefault().unregister(this); }
Now the applications of this is limitless and it sure does give the developer ease and edge of handling data sharing and focusing on other aspects of the application.
Repository
You can see a running example with a complex situation solved using EventBus.
https://github.com/code-crusher/android-demos/tree/master/EventBusDemo
Conclusion
This is just a small part of some EventBus concepts. If you want to learn more about this, checkout the EventBus repo:
https://github.com/greenrobot/EventBus
Twitter: https://twitter.com/Vatsal__Bajpai
Hope this was usefull and it makes your application better. See you soon in the upcoming stories :)