Skip to content
Related Articles

Related Articles

How to Implement EventBus With RxJava – RxBus?

Improve Article
Save Article
Like Article
  • Last Updated : 04 Oct, 2021

Prior to RxJava, we used Bus libraries like Otto, EventBus, startActivityForResult Intent, and others to update other fragments on behalf of the current job. Let’s say we started the first activity, then another, then another, and so on until the fourth activity is in the foreground and the other three are in the background. If we make any sort of change from foreground activity and then return to background activity and load data again to refresh the UI with new data, we no longer need to do this; we no longer need to make any further requests to the server. Simply create a RxBus event and listen wherever you wish to listen and update the UI.

When we use RxJava to implement the EventBus paradigm, we refer to it as RxBus.

Note: Please keep in mind that this is only an example & you must be familiar with the subject in RxJava.

Image 1. Understanding the RxJava –RxBus

Without further ado, let’s start by creating RxBus.

Step by Step Implementation

Step #1: Creating RxBus

The first step is to create an RXBus through which we will drive all the UI changes, we call it GfgRxGfgSampleBus(), through this, we can access and make changes accordingly.


public class GfgRxGfgSampleBus {
    public GfgRxGfgSampleBus() {
    private CoursePublications<Object> gfgSampleBus = CoursePublications.create();
    public void send(Object o) {
    public Observable<Object> toObservable() {
        return gfgSampleBus;

Now, we’ll construct a RxBus Singleton (single instance) in our application class or anywhere else, in : 

Step #2: Creating Singleton in application

The Next step is to create a Singelton application, Through this application written in Java, we can access the Rx Bus which we created in the previous step. 


public class GfgSampleApp extends Application {
    private RxSampleBus sampleBus;
    // Declaring an object
    public void onCreate() {
        sampleBus = new RxSampleBus();
    public RxSampleBus sampleBus() {
        return sampleBus;

As a result, we can now access RxBus from anywhere. We can sign up for an event in any class. Which lands us to: 

Step #3: Accessing the signup event

Now we will access the signup event which we created in the previous step and then drive the UI changes which we need using the Rx Bus, using a consumer object, which we will make it One Type Event, simply follow the steps below: 


((SampleGfGApp) getApplication())
        .subscribe(new Consumer<Object>() {
            public void accept(Object gfgObject) throws Exception {
                if (gfgObject instanceof EventTypeOne) {
                    Logger.d("GfG Articles are the best One");
                } else if (gfgObject instanceof EventTypeTwo) {
                    Logger.d("GfG Articles are the best Two");


Finally, we have the EventBus pattern with RxJava and RxBus. However, if there is a problem, it will terminate, therefore check RxRelay to avoid this. 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!