DroidconMVVM icon indicating copy to clipboard operation
DroidconMVVM copied to clipboard

Survive orientation changes

Open romtsn opened this issue 7 years ago • 3 comments

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.

romtsn avatar Jun 26 '17 09:06 romtsn

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

pwittchen avatar Aug 26 '17 21:08 pwittchen

A view model available synchronously after rotation will solve that.

cloudshooterhuman avatar Apr 24 '18 15:04 cloudshooterhuman

I was asked the same question during an interview and the interviewer suggested to use Loaders, I haven't tried them yet though.

venkateshreddy74 avatar Aug 27 '18 02:08 venkateshreddy74