question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`rememberSaveable` does not work - restore/save logic is never called

See original GitHub issue

I 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:open
  • Created 2 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
hakanaicommented, Mar 17, 2022

I partially agree, except:

  1. 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 their singleWindowApplication function, i.e. the very first line of code is Compose code.

  2. 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.

0reactions
hakanaicommented, Sep 4, 2022

No… I didn’t have the slightest idea where to start.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found