In some cases my apps state is not saved/restored ...
# compose
z
In some cases my apps state is not saved/restored after the app has been in the background for a while (<5 min). Ive reported it as a bug, but Im having a hard time understanding how it could be a bug - so many apps would break, and I probably wouldnt be the first to run into it. So, am I doing something wrong? Code & more details in thread 🧵
Copy code
val registry = LocalSaveableStateRegistry.current
val key = "boop"

val bundle = registry?.consumeRestored(key) as? Bundle

//TODO: bundle is used in my applications navigation

DisposableEffect(Unit) {
    val entry = registry?.registerProvider(key) {
        Bundle() // Filled with precious data
    }

    onDispose {
        entry?.unregister()
    }
}
This is what Im doing in the root composable, and it works most of the time, but not always. Could it be that
onDispose
is called before the app has managed to save its state (under certain circumstances)? https://issuetracker.google.com/issues/221190158
Reports are from users with Samsung, Huawei, Shift and One Plus phones. Disabling battery optimizations and the like doesnt seem to have any effect. It works flawlessly on my Pixel, as expected I guess.
a
SaveableStateRegistry.consumeRestored()
is a function with side effect so you shouldn't use it directly in a composable function. You should use
val bundle = remember(registry) { registry?.consumeRestored(key) as? Bundle }
.
🙏🏽 1
In your code basically if the root composable is recomposed
bundle
will become null.
z
Sorry, Im actually using remember - just forgot to include in the sample above. I do have a number of keys specified though, which could be breaking functionality just like you describe (thank you). Ill adjust that, but I dont think thats whats causing this - the keys are currently static in my app, and the last one is
rememberCoroutineScope()
which I dont think will change either?
Thank you so much @Albert Chang, I added what you suggested alongside another tweak, and it now works as expected. Some more details in my issue if youre interested!