Lecture on Reactive programming on Android, mDevCamp 2016.
A practical guide to using RxJava on Android. Tips for improving your app architecture with reactive programming. What are the advantages and disadvantages of using RxJava over standard architecture? And how to connect with other popular Android libraries?
80. RxLifecycle
▪ auto unsubscribe based on Activity/Fragment lifecycle
myObservable
.compose(RxLifecycle.bindUntilEvent(lifecycle,
ActivityEvent.DESTROY))
.subscribe();
myObservable
.compose(RxLifecycle.bindActivity(lifecycle))
.subscribe();
81. RxLifecycle
▪ obtain ActivityEvent or FragmentEvent by:
A. rxlifecycle-components + subclass RxActivity, RxFragment
B. Navi + rxlifecycle-navi
C. Write it yourself
82. RxLifecycle
public class MyActivity extends RxActivity {
@Override
public void onResume() {
super.onResume();
myObservable
.compose(bindToLifecycle())
.subscribe();
}
}
83. RxLifecycle
public class MyActivity extends NaviActivity {
private final ActivityLifecycleProvider provider
= NaviLifecycle.createActivityLifecycleProvider(this);
@Override
public void onResume() {
super.onResume();
myObservable
.compose(provider.bindToLifecycle())
.subscribe(…);
}
}
99. Retrofit
– onNext() with Response, then onComplete()
– onError() in case of error
@GET("/data")
Observable<Response> getData(
@Body DataRequest dataRequest);