class ChildState {
// ....
companion object {
val Saver: Saver<ChildState,*> = // Has been defined
}
}
class ParentState(val childState: ChildState) {
// ...
companion object {
val Saver: Saver<ParentState,*> = // How to save the childState using ChildState.Saver?
}
}
t
tad
07/29/2021, 4:15 AM
with(ChildState.Saver) { save(childState) }
a
Albert Chang
07/29/2021, 4:26 AM
Basically you should do something like this:
Copy code
class ParentState(val childState: ChildState) {
// ...
companion object {
fun Saver(childState: ChildState): Saver<ParentState, *> = Saver(
save = { /* Save private data */ },
restore = { ParentState(childState) }
)
}
}
@Composable
fun rememberParentState(): ParentState {
val childState = rememberSaveable(saver = ChildState.Saver) { ChildState() }
return rememberSaveable(childState, saver = ParentState.Saver(childState)) {
ParentState(childState)
}
}