简体   繁体   中英

How to handle rotation with Retrofit and RxJava/RxAndroid in Activity?

I read here that we can use some global cache in order to handle rotation.

You can prevent this by using the cache or replay Observable operators, while making sure the Observable somehow survives the Activity life-cycle (for instance, by storing it in a global cache, in a Fragment, etc.)

But how to achieve that? Somebody can point me to some example how to do it? Or maybe you know some better approach to handle configuration change?

EDIT:

For now I have found many ways, but I ended up with using RxBus realisation. Nice example of using RxBus you can find here .

I have made a demo application ( https://github.com/pmellaaho/RxApp ) in order to experiment how tackle the kinds of situations in Android application. To put it short 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.

I've recently used Loader combined with ConnectableObservable to handle better rotations during RxJava2 stream. I've even created a Medium Post explaining in details my approach.

Basically, you have to put the observable ( ConnectableObservable ) in a Loader with a Transformer , and next re-subscribe to it when activity/fragment resume after being destroyed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM