Reputation: 368
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:
Implement the map interface which will execute the LongRunningProcess:
.map(new Function<String, String>() {
@Override
public String apply(String cad){ // cad is the input value
Log.d(TAG,"long operation in thread: "+Thread.currentThread().getName()); //output thread name
return " longNetworkOperation result => "+doLongNetworkOperation(); //here we do the background operations
}})
Get the results in Main Thread: .observeOn(AndroidSchedulers.mainThread())
Add the observer which get notified for the results: .subscribe(observer);
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
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.
I am currently following the second approach in my application.
Upvotes: 0