![]() |
VOOZH | about |
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.
Without further ado, let's start by creating RxBus.
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.
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.
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:
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.