Limits of rememberSaveable Across Android Auto Backup Restore
0 reputation · 14 Jul 2020, 10:07 UTC
0 reputation · 14 Jul 2020, 10:07 UTC
When an Android app is restored from Auto Backup, Compose UI state held by rememberSaveable is not recovered. The SavedStateRegistry that backs rememberSaveable is excluded from the backup payload, so any counter, scroll position, or form input stored this way resets to its initial value after reinstall. Persistent storage such as DataStore or SharedPreferences is included in the backup by default and survives restore.
The goal is to understand the exact boundary of this behavior and what options exist for preserving UI state across a backup-restore cycle. Does a custom Saver that writes to persistent storage effectively bridge the gap, or does the restore timing make that unreliable? Are there differences between Android Auto Backup and adb backup restore flows that affect rememberSaveable? What is the recommended way to verify that recovered data matches the pre-restore state?
35275 reputation · 14 Jul 2020, 19:49 UTC
When an app is restored via Android Auto Backup, rememberSaveable state is not recovered. The backup process includes only files, SharedPreferences, and SQLite databases; the SavedStateRegistry bundle that backs rememberSaveable is omitted. Consequently, any counter, scroll position, or form input kept only with rememberSaveable resets to its initial value after a reinstall.
android:backup file system entries, SharedPreferences, and SQLite databases.Bundle used by SavedStateRegistry is stored in memory and never written to disk during backup.onRestoreInstanceState bundle; rememberSaveable values are initialised again.ViewModel that writes to SavedStateHandle can survive if the SavedStateHandle is backed by a SavedStateRegistry that writes to disk (e.g., via SavedStateViewModelFactory), but this is not the default behaviour for rememberSaveable alone.Saver that writes to DataStore or SharedPreferences on every change and reads it back in rememberSaveable’s initialValue lambda.
ViewModel + SavedStateHandle. Store UI‑critical data in a ViewModel that writes to SavedStateHandle, which can be backed by a file if you initialise the factory with a SavedStateRegistry that persists to disk.
rememberSaveable for fast in‑app restoration and trigger a background sync to persistent storage on a regular interval or when the counter exceeds a threshold.
Because the backup timing occurs while the app is stopped, a custom Saver that writes to persistent storage before the backup starts will survive. The only caveat is that the app must write the value *before* the backup is triggered; otherwise the value could be lost if the app is killed between state change and backup.
adb backup can include the entire app data directory, which may contain a SavedStateRegistry snapshot if you manually serialise it. Auto Backup never does this.
rememberSaveable behaves the same as after Auto Backup: it is empty.
rememberSaveable value (e.g., increment a counter).onRestoreInstanceState calls and confirm the bundle is empty.To fine‑tune the recommendation, could you confirm whether your app already writes rememberSaveable data to a persistent store (e.g., DataStore) on every change?
Use comments to ask for clarification. Post a solution as an answer.
35,275 reputation · 14 Jul 2020, 17:58 UTC
To clarify the boundary between these systems: rememberSaveable relies on the SavedStateRegistry, which manages a Bundle intended for process death (short-term volatility), not device migration (long-term volatility). While adb backup and Android Auto Backup both target the filesystem, the Bundle is a transient memory object that is never serialized to the backup payload.
When implementing a custom Saver to bridge this gap, be mindful of the restore timing. Data restored via Auto Backup is available as soon as the app is first launched post-install. Therefore, the most reliable pattern is to use the initialValue lambda of rememberSaveable to read from DataStore or SharedPreferences. This ensures the UI state is initialized from the restored persistent storage before the first composition, avoiding a flicker where the UI resets to a default value before jumping to the restored state.