RxLifecycle

项目地址:zhihu/RxLifecycle
简介:Bind observables to the lifecycle of Activity or Fragment in a non-invasive way.
更多:作者   提 Bug   
标签:

Apache 2.0 License Release

This library is a non-invasive version of RxLifecycle. It can help you to automatically complete the observable sequences based on Activity or Fragment's lifecycle. There is an article about how it works.

Supports only RxJava 2 now.

Usage

Use the Transformers provided. bind(your activity or fragment).with(observable type).

RxLifecycle.bind(activity).withFlowable()
RxLifecycle.bind(activity).withObservable()
RxLifecycle.bind(activity).withCompletable()
RxLifecycle.bind(activity).withSingle()
RxLifecycle.bind(activity).withMaybe()

And then compose it to your original observable.

Observable.interval(0, 2, TimeUnit.SECONDS)
        .compose(RxLifecycle.bind(MainActivity.this).<Long>withObservable())
        .subscribeOn(Schedulers.computation())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new Consumer<Long>() {
            @Override
            public void accept(Long n) throws Exception {
                toast("Observable -> " + n.toString());
            }
        });

That's all. You needn't to extend your activity or fragment.

You can also observe the lifecycle events by using the .asFlowable() or .asObservable() methods to convert the RxLifecycle to a Flowable or Observable.

RxLifecycle.bind(this)
        .asFlowable()
        .subscribe(new Consumer<Integer>() {
            @Override
            public void accept(@LifecyclePublisher.Event Integer event) throws Exception {
                switch (event) {
                    case LifecyclePublisher.ON_START:
                        toast("Your activity is started.");
                        break;

                    case LifecyclePublisher.ON_STOP:
                        toast("Your activity is stopped.");
                        break;
                }
            }
        });

In addition, you can also bind observables to the FragmentManager or LifecyclePublisher.

Sample

Check out the sample for more detail.

Using with gradle

  • Add the JitPack repository to your build.gradle repositories:
repositories {
    // ...
    maven { url "https://jitpack.io" }
}
  • Add the core dependency:
dependencies {
    compile 'com.github.nekocode.rxlifecycle:rxlifecycle:{lastest-version}'
}
  • (Optional) Add the below library if you need to support api 9 and later. Besides, if you already add support-v4 dependency, I will also suggest you to use this compact library, and then use the RxLifecycleCompact instead of the RxLifecycle.
dependencies {
    compile 'com.github.nekocode.rxlifecycle:rxlifecycle-compact:{lastest-version}'
}

Contributing

  • To contribute with a small fix, simply create a pull request.
  • Better to open an issue to discuss with the team and the community if you're intended to work on something BIG.
  • Please follow Google Java Style Guide.
Android 开发经验分享
用了一年多的理财推荐
Android 开发经验分享
用了一年多的理财推荐