TheMrCodes
03/22/2021, 10:31 PMLocalSaveableStateRegistry
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 🧵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.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()
}
}
}