How to save state that has other saveable state? `...
# compose
k
How to save state that has other saveable state?
Copy code
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
with(ChildState.Saver) { save(childState) }
a
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)
    }
}
Here is a similar example.
👍 1
k
@tad Thanks. However, I can only save the state but can't restore it @Albert Chang Thanks, it's working