DroidconMVVM
DroidconMVVM copied to clipboard
Survive orientation changes
Hi @florina-muntenescu , thanks for this great introduction into MVVM + RxJava world. But I'm wondering now, how it would survive during orientation changes. Assume, we have some long-running background operation, and as your subscribe in onResume()
and unsubscribe in onPause()
, this operation will be interrupted first and then renewed and started again. So what is the right way to not interrupt this operation, but survive during orientation changes?
Thanks in advance.
There's no answer to this question, so I'll try to make one :).
As far as I see, subscribing observables in onResume()
and unsubscribing them in onPause()
method is the general practice in Android apps and after orientation changes, observables are going to be resubscribed. It's done like that on purpose. If you have a really long operation and you don't want to resubscribe it during orientation changes, I think you should put it into a Service
instead of the Activity
. Then, execution of the operation will be independent of Activity
lifecycle. If you want to see the result of the operation or notification that operation is finished in the UI, then you can pass the result from the Service
to the Activity
via Event Bus (e.g. Otto or a custom implementation of event bus with RxJava).
Regards, Piotr
A view model available synchronously after rotation will solve that.
I was asked the same question during an interview and the interviewer suggested to use Loaders, I haven't tried them yet though.