Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxJava: How resume the work of an observable if a configuration change occurs?

I work with RxJava and RxAndroid. How resume the work of an observable if a configuration change occurs (activity rotation, language locale change etc.)?

I need not only resume the work of an observable, also I need save emitted items, when subscriber is unsubscribed and emit all saved items right away after subscription.

I read a ton of articles, but I didn't find the answer to my question.

I found a few examples, but none of them does not solve the problem:

  • https://github.com/alapshin/rxweather
  • https://github.com/kaushikgopal/RxJava-Android-Samples
  • https://github.com/tehmou/rx-android-architecture
  • https://github.com/richardradics/RxAndroidBootstrap
like image 384
ArtKorchagin Avatar asked Jul 29 '15 14:07

ArtKorchagin


2 Answers

You can use one of the ConnectableObservables. Particularly, cache or replay would be handy for this kind of situation.

For example, you can call cache on your observable, unsubscribe it when activity is destroyed, and resubscribe it again after the activity is recreated.

like image 189
Aaron He Avatar answered Sep 24 '22 10:09

Aaron He


I have made a demo application (https://github.com/pmellaaho/RxApp) in order to experiment how to tackle these kinds of situations. Basically, I use a singleton model from Activity to get the response from network. This makes it possible to cache responses, access the data from multiple UI components, subscribe to pending request and also to provide mock data for automated UI tests.

like image 35
pmellaaho Avatar answered Sep 25 '22 10:09

pmellaaho