Open In App

How to Implement EventBus With RxJava – RxBus?

Improve
Improve
Like Article
Like
Save
Share
Report

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.

Java




public class GfgRxGfgSampleBus {
    public GfgRxGfgSampleBus() {
    }
    private CoursePublications<Object> gfgSampleBus = CoursePublications.create();
    public void send(Object o) {
        gfgSampleBus.onNext(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. 

Java




public class GfgSampleApp extends Application {
    private RxSampleBus sampleBus;
    // Declaring an object
    @Override
    public void onCreate() {
        super.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: 

Java




((SampleGfGApp) getApplication())
        .sampleBus()
        .toObservable()
        .subscribe(new Consumer<Object>() {
            @Override
            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");
            }
      }
});


Conclusion

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. 

 



Last Updated : 04 Oct, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads