Rx binding of stock Android Activities & Fragment Lifecycle, avoiding memory leak
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
});
dependencies {
implementation 'com.github.florent37:rxlifecycle:(lastversion)'
}
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()
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 ->
...
});
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 ->
...
});
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 {
}
}
Author: Florent Champigny
Blog : http://www.tutos-android-france.com/
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.