Florent37 RxLifecycle Save Abandoned

Rx binding of stock Android Activities & Fragment Lifecycle, avoiding memory leak

Project README

RxLifecycle

Rx binding of stock Android Activities & Fragment Lifecycle, avoiding memory leak

This library allows one to automatically finish sequences based on Android lifecycle state, This capability is useful in Android, where incomplete subscriptions can cause memory leaks.

You don't need to extends Activity or Fragment

Compatible with all RxJava2 types : Single, Observable, Flowable, Maybe, Completable

mywebservice.searchUsers("florent")
            .observeOn(AndroidSchedulers.mainThread())
            .subscribeOn(Schedulers.io())
            
            //will dispose this call when the activity / fragment destroys
            .compose(disposeOnDestroy(getLifecycle()))
            
            .subscribe(users -> {
                //display users with animation
            });

Buy Me a Coffee at ko-fi.com

Download

dependencies {
    implementation 'com.github.florent37:rxlifecycle:(lastversion)'
}

Listen to activity / fragment lifecycle events

RxLifecycle.with(this)
           .onDestroy()
           .subscribe(event -> 
                /*do what you had to do on view destroy*/
            );
            
RxLifecycle.with(getLifeCycle())
           .onResume()
           .subscribe(event -> 
                /*do what you had to do on view resume*/
            );

Available events :

  • .onCreate()
  • .onStart()
  • .onResume()
  • .onPause()
  • .onStop()
  • .onDestroy()

Automatically Dispose Rx Observables

You can dispose an Rx operation when the activity state changes, for example

Using doOnSubscribe

mywebservice.searchUsers("florent")
            .doOnSubscribe(disposable -> disposeOnDestroyed(this, disposable)
            .subscribe(l -> 
                 ...
            });

Using compose

//With `import static florent37.github.com.rxlifecycle.RxLifecycle.disposeOnDestroyed;`

mywebservice.searchUsers("florent")
            .compose(disposeOnDestroy(this))
            .subscribe(l -> 
                 ...
            });

Availables : disposeOnStop, disposeOnPause, etc...

You can also use enum state

mywebservice.searchUsers("florent")
            .doOnSubscribe(disposable -> disposeOn(this, DESTROY, disposable)
            .subscribe(l -> 
                 ...
            });

Wait until an Activity state

You can pause an Rx chain until it's not on an event, for example wait for activity to be resumed to perform an animation

mywebservice.searchUsers("florent")
          
            //will pause
            .flatMap(value -> onlyIfResumedOrStarted(this, value))
          
            //only if resumed
            .subscribe(value -> 
                 ...
            });

Usage with MVP

You can bind easily your presenter with a lifecycle, example :

public abstract class AbstractPresenter<V extends AbstractPresenter.View> {

    private WeakReference<V> viewReference;

    @CallSuper
    public void bind(LifecycleOwner lifecycleOwner, V view) {
        this.viewReference = new WeakReference<V>(view);

        RxLifecycle.with(lifecycleOwner)
                .onStart()
                .distinct() //once
                .subscribe(x -> start());
    }

    public abstract void start();

    private interface View {

    }
}

Credits

Author: Florent Champigny

Blog : http://www.tutos-android-france.com/

Android app on Google Play Follow me on Google+ Follow me on Twitter Follow me on LinkedIn

License

Copyright 2017 Florent37, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Open Source Agenda is not affiliated with "Florent37 RxLifecycle" Project. README Source: florent37/RxLifecycle
Stars
128
Open Issues
0
Last Commit
3 years ago
License

Open Source Agenda Badge

Open Source Agenda Rating