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
Post a Comment