A frequent problem in Android: Leaks! or API returns on an activity that is no longer displayed.

Now imagine that the call to the .user resource in our API takes 10 seconds to respond, and then the user has changed activity before the call returns. The code contained in the

will be called, will try to modify the text of the loginTextView, which throws an exception!

Stop an observable

Our solution would be to stop the execution of our observable when the activity is no longer displayed.

The .subscribe Rx object method returns an object named Disposablethat we will use to stop processing.

We’ll just call the .dispose method


The CompositeDisposable is sort of a list of Disposables. We will put our elements to stop, and be able to stop all of them with a single call, using .clear ()

Here are some other syntaxes to add our calls to our CompositeDisposable

The idea is to stop our calls in the onStop () or in the onDestroy () of our Android classes