Could we use `LocalSaveableStateRegistry` to store...
# compose-desktop
t
Could we use
LocalSaveableStateRegistry
to store Scrolle state and simular values across the whole application? And if thats the case can anyone describe how it's API is intendet to be used / whats its lifecycle is? Findings in the 🧵
In the last hour I digged deeper into
LocalSaveableStateRegistry
and found out that its already implemeted into an class named
SaveableStateHolder
that implements the exact behaivir I wanted. It saves the
LocalSaveableStateRegistry
by a key and provides it in a local scope inside the content of its
SaveableStateProvider
method.
Here's a small example using this method to create an custom screen navigator that implements an`SaveableStateHolder` to use a SaveableStateRegistry per screen:
Copy code
Box(Modifier.fillMaxSize()) {
    var screen by rememberMutableStateOf("Home")
    val registry = rememberSaveableStateHolder()
    
    val screens = mapOf<String, @Composable () -> Unit>(
        "Home" to {
            Button(onClick = { screen = "Detail" }) {
                Text("To Detail")
            }
            Text("Home")
            var cnt by rememberSaveable { mutableStateOf(0) }
            Button(onClick = { cnt++ }) {
                Text("Count: $cnt")
            }
        },
        "Detail" to {
            Button(onClick = { screen = "Home" }) {
                Text("To Home")
            }
            Text("Detail")
            var cnt by rememberSaveable { mutableStateOf(0) }
            Button(onClick = { cnt++ }) {
                Text("Count: $cnt")
            }
        }
    )
    
    Row {
        registry.SaveableStateProvider(screen) {
            screens[screen]?.invoke()
        }
    }
}