Disposable and the Activity/Fragment Lifecycle

Take a look at various libraries used to unsubscribe from an Observable.


So far, we have overlooked a key component in a reactive stream: the Disposable. A Disposable is returned as a result from a subscription so that we can control when to unsubscribe. This is so that the underlying Observable can stop emitting events. Failure to do so can cause unwanted memory leaks that are hard to trace.

When dealing with the Activity or Fragment lifecycle, a good place to unsubscribe would be when the Activity or Fragment is no longer needed—that is, in the .onDestroy() lifecycle.

Get hands-on with 1200+ tech skills courses.