简介: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.


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


And then compose it to your original observable.

Observable.interval(0, 2, TimeUnit.SECONDS)
        .subscribe(new Consumer<Long>() {
            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.

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

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

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


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}'


  • 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 开发经验分享
C 轮融资近 30 亿元理财推荐
Android 开发经验分享
C 轮融资近 30 亿元理财推荐