One can also use RxLifecycle with Views. It is quite rare to be in need of this functionality, but it can be used for animations or some delayed or periodic actions that are specific to the view shown.
The Views have a very simple lifecycle and the associated methods are as follows:
- onViewAttachedToWindow(): This view is part of the active screen (created in the Activity, for example)
- onViewDetachedFromWindow(): This view is removed from the screen (the Activity is destroyed or the view is removed programmatically)
Binding the Observable (Subscription) to the lifecycle of the view can be done like this:
TextView textView;Observable.interval(1, TimeUnit.SECONDS) .compose(RxLifecycleAndroid.bindView(textView)) .subscribe(); ...