Disposable and the Activity/Fragment Lifecycle
Understand the role of Disposable in RxJava and how to effectively unsubscribe to avoid memory leaks in Android development. Explore best practices for handling subscriptions during Activity and Fragment lifecycles and discover how libraries like RxLifecycle simplify this process.
We'll cover the following...
Disposable
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() ...