Managing Disposables in RxJava 2 — For Android

Cody Engel
3 min readApr 15, 2017

Update: The method outlined below for managing disposables is rather misleading and not intended for use in production apps. While it serves a good purpose as a baseline of example of what you could do, it’s by no means copy + paste friendly. If you are looking for an updated version with a technique for managing disposables in a production friendly way then I strongly suggest reading Managing Disposables in RxJava2 — The Less Bad Version.

Thanks for this picture of disposable items, Unsplash.

We haven’t yet moved over to RxJava 2 at work so forgive me if I’m late to the game on this one. I recently started working more on side projects after work and with that came using RxJava 2. At work we use CompositeSubscriptions for managing the subscriptions that are created so we can easily unsubscribe from everything if the Android overlords decide to destroy our activity. To my surprise (because reading changelogs is not my thing) I could not find this CompositeSubscription in my RxJava 2 projects, instead there was a CompositeDisposable. Also to my surprise my Observer had a new callback…

The New Callback

So it turns out Observer has a callback which goes by the name of onSubscribe, and it passes back a Disposable object. What this means (unless I’m totally doing things wrong) is you can take that Disposable and throw it into a CompositeDisposable. What this…

--

--