is it nessary to use Rxlifecycle with android-arch-lifecycle component?

1.1k Views Asked by At

If android-arch-lifecycle component is used in a app, is it necessary to use Rxlifecycle? or is Rxlifecycle redundant? or If I used Rxlifecycle and RxJava, is it necessary to use android-arch-lifecycle component?

2

There are 2 best solutions below

0
On BEST ANSWER

I believe, if you don't use android architectural component ViewModel, both libraries will be same.

With ViewModel, you can prevent data preparation when device is rotated, meaning ViewModel stays in memory even activity has to restart in response to device rotation.

I don't think such component exists in RxLifecycle library.

0
On

No you don´t need to use RxLifecycle but if you use RxJava it might be useful to use RxLifecycle.

If you use RxJava you have to care about subscribing and unsubscribing yourself. Androids new LiveData (which is similar to the Rx Observables) does this itself by using the android-arch-lifecycle. If you also want a lifecycle awareness in RxJava you can use RxLifecycle. As described on their Github page, RxLifecylce needs a Lifecycle Provider. And one solution generate the Lifecycle Provider is to use the android-arch-lifecycle.

public class MyActivity extends LifecycleActivity {
private final LifecycleProvider<Lifecycle.Event> provider
    = AndroidLifecycle.createLifecycleProvider(this);

@Override
public void onResume() {
    super.onResume();
    myObservable
        .compose(provider.bindToLifecycle())
        .subscribe();
}

Edit: RxLifecycle may have problems in some cases, thats why the creator recomends to use AutoDispose or handle subscrption manually instead. He describes the problems in this post.