O'Reilly logo

Reactive Android Programming by Tadas Subonis

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required