29

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.

DeniSHow
  • 1,394
  • 1
  • 18
  • 30

2 Answers2

5

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.

pmellaaho
  • 792
  • 1
  • 8
  • 15
  • 2
    Thanks for your demo application! But I think we should use RxLifecycle library in order to handle rotation while we are making request. Look at RxAndroid changes and stable release https://github.com/ReactiveX/RxAndroid/blob/80393be5a835374bf9614e89f0629d5650fcdf92/CHANGES.md – DeniSHow Aug 31 '15 at 06:26
  • 1
    I don't have first hand experience on RxLifecycle but I think that it can take care of automatic unsubscription based on lifecycle events for you. You still need to take care of subscribing to pending requests/getting cached data by yourself. Please correct if I'm wrong – pmellaaho Sep 01 '15 at 10:31
  • I think you are right, but I still haven't implement RxLifecycle, so can't correct you now. I hope somebody who has already used RxLifecycle and RxBinding can correct us. – DeniSHow Sep 01 '15 at 12:26
  • 3
    I'm sorry I was wrong about RxLifecycle, it only can take care of automatic unsubscription in order to prevent memory leaks and not handle rotation. It was my misunderstanding. Thanks for your example once again! – DeniSHow Sep 04 '15 at 14:29
  • @pmellaaho how do you handle load more (pagination) – Raghunandan Nov 09 '15 at 10:43
  • That is an interesting question, maybe you should make an new entry for it or take a look at e.g. this code [A simple Rx based pager] (https://gist.github.com/mttkay/24881a0ce986f6ec4b4d) – pmellaaho Nov 14 '15 at 16:26
  • So essentially you just put stuff into a static storage. – Malcolm Jul 20 '16 at 09:28
  • 1
    On orientation change you want to preserve the request and deliver to the new activity. On actual activity destroy, you want to kill the request and disacard the results. Anybody have a solution? – Greg Ennis Nov 23 '16 at 15:25
  • @GregEnnis There is a way, put your NW requests to a Presenter and then use Loader to preserve Presenter during configuration change. You can read more about it [here] (https://medium.com/@czyrux/presenter-surviving-orientation-changes-with-loaders-6da6d86ffbbf#.7tpermrg3) – pmellaaho Nov 30 '16 at 05:51
0

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.

Phil
  • 4,730
  • 1
  • 41
  • 39