Binding to views

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();

Now the Observable will be terminated when onViewDetachedFromWindow() is called on the view.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.47.166