LearningCharlito
LearningCharlito

Reputation: 368

Android RxJava2 Handle Orientation Changed

I'm new at this library, so if you got any tips or suggestions are more than welcome. I got RxJava2 running and executing a longNetworkOperation fake method, the flow is this:

And the observer:

 Observer observer = new Observer() {
    @Override
    public void onSubscribe(Disposable d) { Log.d(TAG,"onSubscribe, thread name >> "+Thread.currentThread().getName()); }

    @Override
    public void onNext(Object value) { Log.d(TAG,"on next, input value:<<"+value.toString()+">>  thread name >>"+Thread.currentThread().getName());}

    @Override
    public void onError(Throwable e) { Log.d(TAG,"error >>"+e.toString()); }

    @Override
    public void onComplete() {
        Log.d(TAG,"onCompleted  thread name >> "+Thread.currentThread().getName());
        tvRxJava = (TextView) findViewById(R.id.tv_rxjava); //update the reference of tvRxJava.
        tvRxJava.setText("process executed..."); //this line does not change the value of the TextView tvRxJava.
    }
};

This works fine, with no device rotation, but if you rotate the device while doLongNetworkOperation is running in the background the TextView tvRxJava does not update value. not even with a new inject.

tvRxJava = (TextView) findViewById(R.id.tv_rxjava); 

How do you manage orientation change in android with RxJava2??

This is my gradle dep:

compile 'io.reactivex.rxjava2:rxjava:2.0.5'//rxjava2
compile 'io.reactivex.rxjava2:rxandroid:2.0.1' //rxjava2 for android

Upvotes: 1

Views: 2067

Answers (1)

Arpit
Arpit

Reputation: 1289

Normally I would like to have cache mechanism for the observable in order to get back the result again in case of configuration changes (which forces activity recreation). I am sharing two very good article which will give you very clear picture, in order to solve this problem.

  1. First
  2. Second

I am currently following the second approach in my application.

Upvotes: 0

Related Questions