0
votes

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:

  • Create the observable: Observable.just("str1")
  • Change the execution thread: .subscribeOn(Schedulers.newThread())
  • 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
1
The issue is not really with RxJava, it's to do with the fact that when the device orientation changes the default action is to destroy and recreate the Activity. The newly created copy of the Activity has no way of receiving data from the RxJava request you created in the old activity. You may be able to avoid this by setting appropriate options in the manifest to allow the activity to handle the orientation change itself, which prevents the Activity being destroyed. - Clyde

1 Answers

0
votes

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.