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 the Fragment lifecycle

RxLifecycle supports Fragments as well, so an identical approach can be taken to fragments:

import com.trello.rxlifecycle2.components.support.RxFragment;public class ExampleFragment extends RxFragment {    @Override    public void onCreate(@Nullable Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        Observable.interval(1, TimeUnit.SECONDS)                .compose(this.bindToLifecycle())                .subscribe();    }}

RxFragment has the same method, .bindToLifecycle(), available but tailored to the lifecycle of the Fragment. Thus, when an Activity and the Fragments that are inside it are destroyed, the Subscriptions will be cleaned up properly.

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