AutoDisposable for RxJava with Lifecycle Architecture Component

AnhVu
MindOrks
Published in
2 min readNov 18, 2018

In this article, I’ll introduce how to create the lifecycle-aware disposable by using Android Architecture Component

My article was posted at Kotlin Weekly #121, Thanks to the Kotlin Weekly Team ^^!

Fire and forget

By the normal way, we handle the Rx subscription by creating a CompositeDisposable and add every Disposable into it, then dispose in the onStop or onDestroy of Fragment/Activity:

//create the compositeDisposable
val compositeDisposable = CompositeDisposable()
...
var disposable = yourObservable.subscribeWith(aObserver)
compositeDisposable.add(disposable)
...

override fun onDestroy() {
super.onDestroy()
compositeDisposable.clear()
}

It is important to release the resources and avoid a lot of unexpected errors. However, It’s very boring and easy to forget.

Android Architecture Component is the Game Changer

The android.arch.lifecycle package provides classes and interfaces that let you build lifecycle-aware components—which are components that can automatically adjust their behavior based on the current lifecycle state of an activity or fragment.

You can find more about Lifecycle Component here and in the Code Lab.
This is the implementation of the AutoDisposable

class AutoDisposable : LifecycleObserver {
lateinit var compositeDisposable: CompositeDisposable
fun bindTo(lifecycle: Lifecycle) {
lifecycle.addObserver(this)
compositeDisposable = CompositeDisposable()
}

fun add(disposable: Disposable) {
if (::compositeDisposable.isInitialized) {
compositeDisposable.add(disposable)
} else {
throw NotImplementedError("must bind AutoDisposable to a Lifecycle first")
}
}

@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
fun onDestroy() {
compositeDisposable.clear()
}
}

AutoDisposable has a CompositeDisposable, that will be disposed when the lifecycle event `ON_DESTROY`.
In your Activity/Fragment(where you subscribe your observable), you need to initialize the AutoDispose and bind the AutoDispose into its lifecycle. Note that the Fragment/Activity has its own lifecycle.
private val autoDisposable = AutoDisposable()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
autoDisposable.bindTo(this.lifecycle)
}

Then you need to add the Disposable into the autoDisposable That’s it, your Disposable is auto-disposed now.

var disposable = observable.subscribe(observer) 
autoDisposable.add(disposable)

You can make your code even better by using the Extension function of Kotlin.

fun Disposable.addTo(autoDisposable: AutoDisposable) {
autoDisposable.add(this)
}

Then your Subscription code looks really cool:

observable
.subscribeOn(Schedulers.io) //Do something else
.observeOn(AndroidSchedulers.mainThread())
.subscribe(observer)
.addTo(autoDisposable) // <- magic here

Thank you for reading. You can find the full source code here

--

--

AnhVu
MindOrks

Android Developer @ SeaGroup | Kotlin and RxJava leaner