Android RxJava in 5 minutes

Costa Fotiadis
4 min readFeb 8, 2019
A typical reaction to googling RxJava

These go(o)gles do noffing!

RxJava (and Dagger) seem to be everywhere in the Android space at the moment. Yet a simple googling when trying to find a tutorial that makes sense doing the simplest thing will promptly result in tears.

What is reactive programming? Observable? Operators? Hot / cold?

Do i have to give credit in these?

It looks like everyone is writing “pro” guides on how to do weird stuff in RxJava (i just want to do a for loop) that you would never have to do in a typical android project.

Well I’m a dummy who can’t read more than 4 lines without pictures in between so why not just jump straight into it?

What you will need

Just click File -> New project in Android Studio 3 and include Kotlin support and an empty activity pre-made. Press next on everything. Jetbrains really understands its market (monkeys banging the keyboard like me) and basically writes everything for you these days.

Let the thing finish building.

Dependencies

Get yourself a build.gradle (Module: app) looking like this:

Sync your project.

5 more minutes mom!

Just go to your empty activity and declare this variable before your onCreate() method.

private val compositeDisposableOnPause = CompositeDisposable()

B-but you gotta know the theory. It’s important!

Sure it is. Alas, it’s out of scope of this thing I wrote instead of going out on a Friday night.

Override the onPause() function of your activity.

Time to actually do something now.

Here be dragons

The main selling point of RxJava is that it makes it easier to do something in another thread (plus, it’s cool).

Any code that “touches” the screen (like changing the color of a button) should always be run on the (default) UI thread. Everything else is, for the most part, up to you.

Get a button created in your activity’s xml.

Set a click listener. Your onCreate() method should look like this.

Now, inside there’s a method called changeButtonText().

I’m sure this makes no sense.

There’s a generateString() method in there too:

Obviously this returns just “reddit” without really doing anything complex.

Run this thing and try it out. Once you click the button its text should change to “REDDIT”.

That’s it. You just did an asynchronous operation and you can now setup a LinkedIn profile calling yourself a rock star developer.

Ultra mega boring explanation

Guess we gotta go through the code and pretend this is an actual tutorial.

compositeDisposableOnPause.add(
Single.fromCallable {generateString()}

The compositeDisposableOnPause variable we created is kind of a basket where you can put things in it.

Single.fromCallable {generateString()}

This is one of those things. There’s loads of different ones like some weird D&D spell system.

We added this thing in the basket. If you look up a few lines, on the activity’s on pause there’s this line:

compositeDisposableOnPause.clear()

This means that when the activity’s pause is triggered, everything in this basket will die a horrible death if it’s running and the basket will be emptied.

If the generateString() method took 10 seconds to complete (impossible on this example but if you do something very time consuming instead it very well could) and the onPause() was triggered on the fifth second then the generateString() method will be killed and never completed. The button’s text will remain as it was without changing.

.subscribeOn(Schedulers.io())

This means that the generateString() method will be run on the IO scheduler. The details don’t really matter. We only care that another thread will be suckered into doing the work and not our precious UI thread.

.observeOn(AndroidSchedulers.mainThread())

The result of the generateString() method will come back on the UI thread (or otherwise called AndroidSchedulers.mainThread()).

.subscribe { textGenerated -> button.text = textGenerated }

We called the resulting string “textGenerated”. You can really call it anything. Then an arrow pointing right. Cool.. okay. This means that you just wanna run this text changing bit of code along with the textGenerated string you just got. This is an operation that “touches” the screen so it needs to be done in the UI thread.

The end?

wew lad you made it

Check out some more RxJava in the unofficial sequel here:

Smash like and subscribe guys new videos every Wednesday

--

--