Answer
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.
Confirmed Facts
- Auto Backup serialises
android:backup file system entries, SharedPreferences, and SQLite databases.
- The
Bundle used by SavedStateRegistry is stored in memory and never written to disk during backup.
- After a restore, the app receives an empty
onRestoreInstanceState bundle; rememberSaveable values are initialised again.
- State stored in a
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.
Typical Work‑Arounds
- Persist the value manually. Use a
Saver that writes to DataStore or SharedPreferences on every change and reads it back in rememberSaveable’s initialValue lambda.
- Use
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.
- Hybrid approach. Keep
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.
Differences Between Auto Backup & ADB Restore
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.
- ADB restore runs in the same process state as a fresh install, so
rememberSaveable behaves the same as after Auto Backup: it is empty.
Verification Checklist
- Enable Auto Backup on the test device.
- Open the app, change a
rememberSaveable value (e.g., increment a counter).
- Trigger a backup via Settings → Backup.
- Uninstall the app, then reinstall and restore from backup.
- Verify the counter has reset to its initial value.
- Check Logcat for
onRestoreInstanceState calls and confirm the bundle is empty.
Missing Diagnostic Detail
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?