Simplest RxJava Tutorial for android!!

Vipul Thakur
BYJU’S Exam Prep Engineering
3 min readNov 8, 2018

Well after reading this article I can make sure that you would start using RxJava in your code right away. So let’s get started…

RxJava is java implementation for Reactive programming (Event Based Programming) which in simple terms means that one part of your code would fire an Event(a button click, successful api response etc) while some other part of code would observe and react to that particular event asynchronously(updating view, handling the api result etc).

The OLD WAYS !!

If you have worked in android you obviously have worked with AsyncTask class. The only task it accomplished was to switch from the UI thread, do some work on a background thread and return the result on to the main thread.

But it also came with some problems:

  1. Boilerplate code
  2. Multithreading was a nightmare
  3. More error prone
  4. Memory/context leaks are easily created is an inner class and thus holds an implicit reference to the outer class
  5. No error handling

RxJava to the rescue!!

Observable{
//The work you need to do
}
.subscribeOn(Schedulers.io) //thread you need the work to perform on
.observeOn(AndroidSchedulers.mainThread()) //thread you need to handle the result on
.subscribeWith(Observer{
//handle the result here
})

Well, that’s it, yes it’s that simple!!!

Observable

If you remember RxJava is Event based programming so Observable is that part of the code which fires up the event. You listen to observables for appropriate events.

Observer

The part of the code which listens and react to the event fired from the Observable. You need to override its interface methods for the Observer to work.

  1. onSubscribe(): Method will be called when an Observer subscribes to Observable.
  2. onNext(): This method will be called when Observable starts emitting the data.
  3. onError(): In case of any error, onError() method will be called.
  4. onComplete(): When an Observable completes the emission of all the items, onComplete() will be called.

Schedulers

To link the observer to observable and mentioning the threads to work and handle the result on (subscribeOn() & observeOn())

Let’s look it in action

First, you need to add the dependency.

implementation "io.reactivex.rxjava2:rxjava:2.1.6"
implementation "io.reactivex.rxjava2:rxandroid:2.0.1"

Printing a simple array list and updating the recycler view adapter.

ArrayList animals = new ArrayList();
animals.add("Tiger");
animals.add("Lion");
animals.add("Elephant");
Observable.just(animals)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(new Observer<ArrayList>() {
@Override
public void onSubscribe(Disposable d) {

}
@Override
public void onNext(ArrayList arrayList) {
//handling the result
adapter.updateList(animals);
adapter.notifyDataSetChanged();
}
@Override
public void onError(Throwable e) {
//error handling made simple
}
@Override
public void onComplete() {
//cleaning up tasks
}
});

As you can see how RxJava has helped us overcome problems with handling background tasks using AsyncTask class.

  1. Less code
  2. Multithreading (will be explained in upcoming tutorials)
  3. Minimized error occurrences
  4. No memory leaks
  5. And a way to handle the error at one single point(on Error()).

Wrapping up

I hope this tutorial was helpful to you and have motivated you to dump the old way of handling background tasks and switching to RxJava from now on. We will be learning more about RxJava in the upcoming tutorials like different types of Observable and Observers, Disposables, handling multithreading and much more. You might still have some doubts about using RxJava but to overcome them I suggest is to just go ahead and start using it. The more you will use RxJava the more you will be familiar and comfortable with it.

Some useful resources

  1. Nothing beats the official documentation: http://reactivex.io/tutorials.html
  2. Youtube tutorial: https://www.youtube.com/watch?v=k3D0cWyNno4&t=1769s

--

--