google/agera

agera and screen rotation

dimkinware opened this issue · 4 comments

Hi

How I can handle screen rotation with agera repository?
For example, I create repository in activity onCreate(), do addUpdatable() in onResume() or onStart(), and repository start getting data from network very slowly on worker thread.
Then activity recreated after screen rotation and, how I understand, new repository will be created in onCreate() and another one request will be executed. But here I want get result from first repository.
Maybe I must only save created repository in global variable?

Thanks!

Depending on the implementation of the repository. If it is a custom repository and it has quick access to a cached storage of the data, then it doesn't hurt too much to recreate it when the activity restarts -- just let it be initialized to the cached value if available.

Otherwise, you can consider using a UI-less fragment that retains its instance to keep the repository across activity restarts. The repository will likely still go through a deactivation and reactivation cycle, but any value from the last completed request can stay in the repository for use by the restarted activity instance. It can even keep the ongoing unfinished network request (only if this is appropriate) by not terminating the request when it is deactivated.

If this repository can likely benefit from being shared by multiple components (different activities and services) in your app, you can also consider making it a "global" (globally accessible instance), by means of static fields. But this decision needs to go through careful thinking -- you might end up retaining too much memory when the repository is not needed.

"The repository will likely still go through a deactivation and reactivation cycle": Just did a change for this for the future 1.1.x release that will keep the repositories alive if deactivation and reactivation is registered in the same loop on the looper thread, meaning they will survive rotation and thus not do this extra update.

Would agera repositories play well with Android loaders? Did anyone tried or is there such a sample?

LouisCAD, how you load data is irrelevant in that case. Agera makes it easy to get off the main thread, which is one reason to use Loaders. The other reason is surviving rotation, but to do this you can put the repositories in a retained fragment or in a scope outside the config change lifecycle as Max explained.