rx-java Android with RxJava RxAndroid - AndroidSchedulers


Example

This is literally the only thing you need to start using RxJava on Android.

Include RxJava and RxAndroid in your gradle dependencies:

 // use the last version
compile 'io.reactivex.rxjava2:rxjava:2.1.1'
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'

RxAndroid main addition to RxJava is a Scheduler for the Android Main Thread or UI Thread.

In your code:

Observable.just("one", "two", "three", "four", "five")
        .subscribeOn(Schedulers.newThread())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(
            data -> doStuffOnMainThread(),
            error -> handleErrorOnMainThread()
        )

Or you can create a Scheduler for a custom Looper:

Looper backgroundLooper = // ...
Observable.just("one", "two", "three", "four", "five")
        .subscribeOn(Schedulers.newThread())
        .observeOn(AndroidSchedulers.from(backgroundLooper))
        .subscribe(
            data -> doStuffOnMainThread(),
            error -> handleErrorOnMainThread()
        )

For most everything else you can refer to standard RxJava documentation.