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 Activity Lifecycle

The simplest way to use RxLifecycle in your code is to make your activities extend RxActivity:

import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;public class ExampleLifecycleActivity extends RxAppCompatActivity {}

Since we've extended RxActivity, we now have an access to the bindToLifecycle() method that can be used to terminate the Subscription. It can look something like this:

public class ExampleLifecycleActivity extends RxAppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_mock);        Observable.interval(1, TimeUnit.SECONDS)                .compose(bindToLifecycle())                .subscribe();    }}

The key call here is ...

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