The following is the first few sections of a chapter from Android's Architecture Components, plus headings for the remaining major sections, to give you an idea about the content of the chapter.
RxJava is cool, albeit confusing. But beyond that, it is a pure Java library. RxJava knows nothing about Android-specific concepts, as it is designed to be used on all sorts of Java projects.
Android developers using RxJava invariably also add RxAndroid, which gives us
access to a
Scheduler that knows about the Android main application thread.
However, RxAndroid does not have anything that deals with activity or fragment
lifecycles, leaving that up to you.
With Android lifecycles, we want to create things as activities and fragments
start up and clean up those things as the activities and fragments go away.
In the case of RxJava, if we subscribe to some
Observable, it would be nice
to get rid of that subscription at an appropriate point.
In this chapter, we will explore a few options — including one from the Architecture Components — for dealing with lifecycles with RxJava.
The default way of handling this is the approach used in the chapter on RxJava and Room:
Disposablethat you get back from subscribing to an observable
Disposablein a suitable lifecycle method, such as
onDestroy(), via a call to
If you have several subscriptions to track,
CompositeDisposable lets you
track all of them in one spot.
methods to add subscriptions to it. And, as the name suggests,
implements the composite pattern, and so
CompositeDisposable itself is a
dispose() on the
CompositeDisposable triggers calls
dispose() on all of the
Disposable objects you added to the composite.
This works, but it does require you to remember to clean these things up, and it is easy to forget.
The preview of this section was stepped on by Godzilla.
The preview of this section left for Hollywood to appear in a reality TV show.