RxLifecycle
This library handles disposing of Observer on different lifecycle events.
Usage
Extend Activity from RxActivity
public class MainActivity extends RxActivity {
or Fragment from RxFragment
public class MainFragment extends RxFragment {
Create a Disposable
Disposable d = Observable
.interval(1, TimeUnit.SECONDS)
.subscribeWith(new DisposableObserver<Long>() {
@Override
public void onNext(Long value) {
Log.d(TAG, value.toString() + " Seconds");
}
@Override
public void onError(Throwable e) {
Log.e(TAG, "onError", e);
}
@Override
public void onComplete() {
Log.d(TAG, "onComplete");
}
}
);
Now if you want to dispose Disposable "d" on a specific event - e.g., onResume
this.dispose(d, ActivityEvent.RESUME);
or if you want to dispose Disposable "d" on a opposing lifecycle event - e.g., if subscribing during START, it will terminate on STOP
this.dispose(d);
Installation
maven {
url "https://jitpack.io"
}
and:
dependencies {
compile 'com.github.mahmed8003:RxLifecycle:0.0.4'
}