RxBus — an Event Bus Based on RxJava and Optimized for Android

What it’s all about

Event bus is a great solution that allows objects with different lifecycles and located in different layers of hierarchy to communicate.

I venture to suggest that if you have an event bus in your Android application, most likely you use libraries like Otto or EventBus.

However, Otto from Square was officially deprecated and its page on GitHub says: “This project is deprecated in favor of RxJava and RxAndroid.”

As we already use RxJava and RxAndroid in the development, we decided to try out the Rx approach to implementation of event bus. Of course, we did our best to save a clean api driven by annotations and simple to use bus library like Otto.

This is how RxBus library was made. This library is available on GitHub.

Usage tips

We recommend obtaining a single instance of bus through injection or another appropriate mechanism. Alternatively, you may get a singleton like the following:

By default, the Bus enforces that all interactions occur on the main thread. To subscribe to an event, you can declare and annotate a method with @Subscribe. The method should be public and should take only a single parameter.

You can also add a filter and an alternate enforcement by creating subscription with the following:

To receive events, a class instance would need to register with the bus:

A custom subscriber also needs to register with the bus:

When appropriate, you would also need to call the unregister method:

To publish a new event, you can call the post method:

To add RxBus to your project (as a Gradle dependency), use:

If you have any suggestions on how to improve the RxBus implementation, don’t hesitate to contact us.

We build software and we do it right | Custom web and mobile development | https://anadea.info/

We build software and we do it right | Custom web and mobile development | https://anadea.info/