RxJava has been proven to be a great tool for Android applications. Unfortunately, especially for those who are not familiar with reactive paradigm, it is easy to misuse or at least easy to not exploit all its power.
In this article I am going to focus on quite common way to misuse it, especially among beginners.
A quite common mistake is to create a new stream each time there is a need to send a message.
In order to take advantage of reactive paradigm, we should normally follow this very minimal sequence of steps:
- Start listening to a stream (subscribe)
- Receive events (items from the stream) and react to them
- Stop listening to the stream (unsubscribe)
So basically, when we decide that we are interested in listening for a source of events, we subscribe to it, we keep receiving all events, and just eventually, when we don’t need to listen anymore, we unsubscribe.
Get Giuseppe Villani’s stories in your inbox
Join Medium for free to get updates from this writer.
For the sake of clarity, let’s give a concrete example. Imagine that you need to react to a click on button into a UI. When the button is clicked you want to perform an action and get a result back, in order to update your UI. I saw some implementation, for solving this issue, like this:
public void onSubmit(View view) {
service.sendSomething(request)
.map(data -> manipulate(data))
.observeOn(AndroidSchedulers.mainThread())
.subscribe(result -> updateUi(result));
}The snippet above works fine, except that it doesn’t exploit the powerful of RxJava and reactive paradigm, it is indeed imperative. Each time the user click a button a new subscription is created, a single event is generated from the Observable, and the same event is consumed from our observer. So we end up jumping back and forth inside and outside the stream, basically breaking the chain.
So what is a better way to implement it? Well, the idea is to understand that when we recognise the need of communication between the two components (publisher and subscriber) we should set up (subscribe) to the stream and just see each click as an event on the same stream. There is a very useful library from Jake Wharton (RxBinding) that helps with the translation of a click into an event emitted on the same stream. It basically defines an Observable that emits event, when the click happens.
RxView.click(view)
.flatMap(ignored -> service.sendSomething(request))
.map(data -> manipulate(data))
.observeOn(AndroidSchedulers.MainThread())
.subscribe(result -> updateUi(result));In this way, after we subscribe we don’t care anymore about what we have to be listening for. The “channel” is established, and each item will flow from the source to every components interested (subscribed) to them.
When using RxJava, try to use declarative approach in order to exploit reactive functional capabilities.
In this way we have more control over the flow. For example we can translate a double tap into a different Observable, or we can apply a debounce or throttling operator, or combine this single continuous stream with other streams in order to create more complex functionalities.






