CompositeDisposable with Android Lifecycle Component
When we work with RxJava, we always need to add all the disposables to a CompositeDisposable and you need an extra step to dispose it in
onDestroy() of the activity and fragment.
When Android Lifecycle component is released by Android team, I have an idea that why don’t we bind the lifecycle events with the CompositeDisposable. That’s why I make a demonstration on how we could make any CompositeDisposable or even Disposable dispose according to Android Lifecycle events.
Let say we want to dispose the CompositeDisposable when the activity is being paused:
In the above code, there is a integer emitter emitting signal for every second. When the user pauses the app, for example, sending it to background. The app pauses, the lifecycle event will trigger the
LifecycleCompositeDisposable to dispose all added
Disposable objects. Nice.
It’s just my first tastes on Lifecycle component. They also provide other cool stuff like
Room. All components make Android developers handle the lifecycle and configuration change issue much more simpler than before.