anand gaurav anand gaurav - 1 year ago 1218
Android Question

How to use CompositeDisposable of RxJava 2?

Examples on how to use CompositeDisposable or Disposable in RxJava2. In RxJava 1, there was CompositeSubscription, but that is not present in RxJava2, There is something CompositeDisposable in rxJava2. How to use CompositeDisposable or Disposable in RxJava2.

Answer Source
private final CompositeDisposable disposables = new CompositeDisposable();

// adding an Observable to the disposable
                .subscribeWith(new DisposableObserver<String>() {
                    public void onComplete() {

                    public void onError(Throwable e) {

                    public void onNext(String value) {

    static Observable<String> sampleObservable() {
        return Observable.defer(new Callable<ObservableSource<? extends String>>() {
            public ObservableSource<? extends String> call() throws Exception {
                // Do some long running operation
                return Observable.just("one", "two", "three", "four", "five");

// clearing or unsubscibing

I have a create a sample project to demonstrate the use of RxJava2. Here you can find the sample project - RxJava2-Android-Samples

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download