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.

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

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