How do you Handle Configuration Change ?

Configuration changes such as screen rotation , multi-window, foldable postures, changing locale cause the activity to be recreated, which means all Activity fields are lost.

Core Principal - UI State should not live inside activity.

There are 3 ways to save the data ,

  • ViewModel :

    Hold the UI state in memory and survives configuration change. It has much longer lifespan as the old viewModel can be reused inside new Activity.

  • SaveStateHandle :

    Lightweight, Bundle backed storage. Its fast but size is limited ~1MB.

  • Proto Datastore :

    Disk based persistance.
    Survives process death (Force kill, low memory)
    Safest, ensure state is restored when relaunched.

Comments

Popular posts from this blog

What is a Coroutine? Why is it better than threads?

What are Coroutine Builders?

Sealed Classes and Sealed Interfaces