`rememberSaveable` does not work - restore/save logic is never called
See original GitHub issueI currently have:
val appState = remember { AppState() }
I thought I would try switching to rememberSaveable
so that app settings would persist across app invocations.
So I changed it to:
val appState = rememberSaveable(saver = AppState.Saver) { AppState() }
AppState.Saver
is currently just:
val Saver = object : Saver<AppState, Any> {
override fun restore(value: Any): AppState? {
TODO("Not yet implemented")
}
override fun SaverScope.save(value: AppState): Any? {
TODO("Not yet implemented")
}
}
If I put a breakpoint on those TODO
lines, I can see that they are never called. I’d expect a call to save during application exit, and a call to restore some time during startup.
I assume there’s a way to call the right things programmatically to get this to happen as a workaround but haven’t figured it out yet.
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (2 by maintainers)
Top Results From Across the Web
rememberSaveable loses state on rotate - Stack Overflow
Investigating a little bit, I noticed the following: The problem is really the NavHost. If one moves the line defining the variable "value"...
Read more >rememberSaveable needs default saver [180042685]
I'm not sure if the docs say anywhere that the init() function will only be called the first time and using it to...
Read more >ViewModel and State in Compose - Android Developers
When a configuration change occurs, you can save an app's data through different ways, such as using rememberSaveable or saving the instance state....
Read more >Jetpack Compose Support in Workflow | Square Corner Blog
They are strongly-typed, and know nothing of the view layer itself. ... Any rememberSaveable calls in the composition will use this ...
Read more >Jetpack Compose: remember, mutableStateOf, derivedStateOf ...
Jetpack Compose: remember, mutableStateOf, derivedStateOf and rememberSaveable explained. If you have a composable function you might ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I partially agree, except:
It isn’t clear that Compose is just a “UI framework”. The docs really make it seem like an “app framework” instead - code examples show the
main
function directly calling theirsingleWindowApplication
function, i.e. the very first line of code is Compose code.This means I have to reinvent the wheel even though surely I am not the only person trying to use this framework to make full applications.
So yeah, Compose may be a UI framework and not an app framework, but if that’s the case, things like
rememberSaveable
are confusing at best, and it means we have to wait for someone to make an actual app framework which is built on Compose until we can really develop apps with ease, because “just putting state in some object and serialising it” is not trivial.No… I didn’t have the slightest idea where to start.