mcharmas/shoppinglist-clean-architecture-example

How are orientation changes handle in your app?

Opened this issue · 4 comments

What happens if a user changes the orientation and your request finishes between onDestory of the old Activity and onCreate of the new Activity? Is there a way RxJava can handle that use case?

Good question. To be honest I am looking for perfect solution for this problem since the beginning of my android development. I tend to use RxJava in all application layers - it really simplifies everything. What I always do is unsubscribe all subscriptions onStop and resubscribe what is necessary after screen rotation or when app is brough back from background to avoid memory leaks.

Most of data caching is usually where data repositories are but sometimes it is necessary to do some caching in UI. I keep there observables with cache operator applied. This cache is stored in ratin fragment. Of course everything is covered behind some abstractions and injected by DI.

To answer your question lets say thats the flow:

  • onCreate
  • onStart - staring data fetching (subscribing)
  • onStop - unsubscribing (data still fetchning)
  • onDestroy
  • onCreate
  • onStart - subscribing again - if data is in cache already it will be returned immediately, otherwise request will be done again unless you do sth smart like use cache operator and keep source observable or implement you own mechanics to handle such situations
    ....

I guess there is no silver bullet here...

Ok thank you for clarification. I thought you have maybe found the holy grail...

I tend to use RxJava in all application layers...

So all your use cases are returning Observables? What do you do if your use case needs to fetch the current location (LocationManager)? Could you maybe explain that more in detail? Some code snippets would help a lot ;). For example a use case wich fetches a location, checks if the location is in Europe and then get soma extra data from a REST-Service. I would really appreciate that..

There was a big update of the example app. If you are interested take a look at presenters implementation. They configuration changes now.