Home » Android » android – Do I need to check isDisposed() before calling dispose()?

android – Do I need to check isDisposed() before calling dispose()?

Posted by: admin June 15, 2020 Leave a comment


I’m learning RxJava and I noticed a lot of the sample code does a isDisposed() check before calling dispose(). I did not notice any issues when I called dispose() on an already disposed Disposable.

So my question is, do I need the isDisposed() check? Are there situations where I should check isDisposed() before disposing? What are the pros and cons on doing the check first?

How to&Answers:

It makes little sense to call isDisposed. dispose implementations already do that for you and make sure repeated calls are no-ops or have no detectable effect.

Unfortunately, somebody in the early days of RxJava started writing examples with it and now everybody keeps copying that pattern.

It makes a little more sense to check isDisposed before calling onNext for example but you don’t get to do that very often outside Observable.create().


I don´t think so, if you check i.e. implementation of CompositeDisposable(of course if you using that class, not another implementation of disposable interface)
there is :

    public void dispose() {
        if (disposed) {
...rest of method body
    public boolean isDisposed() {
        return disposed;

So the answer is it depends in what way Disposable interface is implemented, You can check it in JetBrains IDE by right click and go to> implementation


dispose is basically for closing the stream of your subscription. you’re checking isdisposed() will tell you if the existing stream is active. if any relevant data passing through that stream it will be cut.

Ofcors, you can call dispose anytime.