A disposable (previously called Subscription in RxJava 1.0) is a tool that can be used to control the life cycle of an Observable. If the stream of data that the Observable is producing is boundless, it means that it will stay active forever. It might not be a problem for a server-side application, but it can cause some serious trouble on Android. Usually, this is the common source of memory leaks. This will be discussed in-depth in the upcoming chapters.
Obtaining a reference to a disposable is pretty simple:
Disposable disposable = Observable.just("First item", "Second item")
.subscribe();
A disposable is a very simple interface. It has only two methods: dispose() and isDisposed() .
The dispose() element can be used to cancel the existing Disposable (Subscription). This will stop the call of .subscribe()to receive any further items from Observable, and the Observable itself will be cleaned up.
The isDisposed() method has a pretty straightforward function--it checks whether the subscription is still active. However, it is not used very often in regular code as the subscriptions are usually unsubscribed and forgotten.
The disposed subscriptions (Disposables) cannot be re-enabled. They can only be created anew.
Finally, Disposables can be grouped using CompositeDisposable like this:
Disposable disposable = new CompositeDisposable(
Observable.just("First item", "Second item").subscribe(),
Observable.just("1", "2").subscribe(),
Observable.just("One", "Two").subscribe()
);
It's useful in the cases when there are many Observables that should be canceled at the same time, for example, an Activity being destroyed.