Timo Drick
09/02/2020, 11:47 AMvar checkBoxState by savedInstanceState { false }
Is not using the provided Registry :-(
log("${item.data} saved state: $${item.savedState}")
val restoredRegistry by remember(item.savedState) {
log("${item.data} Create new registry with: ${item.savedState}")
mutableStateOf(UiSavedStateRegistry(restoredValues = item.savedState, canBeSaved = { true }))
}
Providers(UiSavedStateRegistryAmbient provides restoredRegistry) {
log("${item.data} ${UiSavedStateRegistryAmbient.current}")
children(item.data)
onDispose {
val saved = restoredRegistry.performSave()
log("${item.data} onDispose saved: $saved")
item.savedState = saved
}
}
Maybe someone can point me into the right direction what is wrong?Zach Klippenstein (he/him) [MOD]
09/02/2020, 12:09 PMMutableState
since you're not changing the reference.Timo Drick
09/02/2020, 12:12 PMZach Klippenstein (he/him) [MOD]
09/02/2020, 12:14 PMonDispose
callbacks are invoked deepest-first, so your onDispose
will be executed after all the `savedInstanceState`s in children
are disposed, and they will remove their state providers from the registry on disposal. So by the time you execute performSave
, the registry will be empty.
See what ChildSavedStateRegistry
does here: https://github.com/zach-klippenstein/compose-backstack/blob/aa24271797de6f1fcae73f58c78e6646be75f5eb/compose-backstack/src/main/java/com/zachklipp/compose/backstack/Backstack.kt#L247Timo Drick
09/02/2020, 12:16 PM[Transition.kt:170] TransitionKt$Crossfade3$showItem$1$1.invoke : com.example.composeplayground.MainUIScreen$StartScreen@5f7ad68 saved state: ${1711518371=[true], 1473374894=[Test]}
[Transition.kt:176] TransitionKt$Crossfade3$showItem$1$1$1.invoke : com.example.composeplayground.MainUIScreen$StartScreen@5f7ad68 androidx.compose.runtime.savedinstancestate.UiSavedStateRegistryImpl@2f9636f
[MainActivity.kt:113] MainActivity$MainView$1.invoke : Transition Render main com.example.composeplayground.MainUIScreen$StartScreen@5f7ad68
[Transition.kt:170] TransitionKt$Crossfade3$showItem$1$1.invoke : com.example.composeplayground.MainUIScreen$StartScreen@5f7ad68 saved state: ${1711518371=[true], 1473374894=[Test]}
[Transition.kt:176] TransitionKt$Crossfade3$showItem$1$1$1.invoke : com.example.composeplayground.MainUIScreen$StartScreen@5f7ad68 androidx.compose.runtime.savedinstancestate.UiSavedStateRegistryImpl@2f9636f
[MainActivity.kt:113] MainActivity$MainView$1.invoke : Transition Render main com.example.composeplayground.MainUIScreen$StartScreen@5f7ad68
[Transition.kt:180] TransitionKt$Crossfade3$showItem$1$1$1$1.invoke : com.example.composeplayground.MainUIScreen$StartScreen@5f7ad68 onDispose saved: {1711518371=[true], 1473374894=[Test], 1629430880=[true], 926054518=[Test]}
[MainActivity.kt:110] MainActivity$MainView$1$1$1.invoke : Transition main dispose: com.example.composeplayground.MainUIScreen$StartScreen@5f7ad68
Zach Klippenstein (he/him) [MOD]
09/02/2020, 12:21 PMbecause the registry contains the hashes from the disposed composition and when it gets recreated maybe the hash will change.This shouldn't happen AFAIK, but did you verify this?
Timo Drick
09/02/2020, 12:21 PMcurrentComposer.currentCompoundKeyHash
Is not the same when the same composable is created againZach Klippenstein (he/him) [MOD]
09/02/2020, 12:28 PMkey
composables.Timo Drick
09/02/2020, 12:29 PMZach Klippenstein (he/him) [MOD]
09/02/2020, 12:30 PMkey
to fix thisTimo Drick
09/02/2020, 12:32 PM@Composable
fun showItem(item: CrosstransitionAnimationItem<SavedStateData<T>>?) {
item?.let { (item, transition) ->
key(item) {
transition(item) {
log("${item.data} saved state: $${item.savedState}")
val restoredRegistry by remember(item.savedState) {
log("${item.data} Create new registry with: ${item.savedState}")
mutableStateOf(UiSavedStateRegistry(restoredValues = item.savedState, canBeSaved = { true }))
}
Providers(UiSavedStateRegistryAmbient provides restoredRegistry) {
log("${item.data} ${UiSavedStateRegistryAmbient.current}")
children(item.data)
onDispose {
val saved = restoredRegistry.performSave()
log("${item.data} onDispose saved: $saved")
item.savedState = saved
}
}
}
}
}
}
Stack {
ts.invalidate = invalidate
showItem(item = ts.itemA)
showItem(item = ts.itemB)
}
Zach Klippenstein (he/him) [MOD]
09/02/2020, 12:35 PMtransition()
do?Timo Drick
09/02/2020, 12:38 PMZach Klippenstein (he/him) [MOD]
09/02/2020, 12:52 PMTimo Drick
09/02/2020, 12:58 PM@Composable
fun <T: Any>NavigationSwitcher(transition: Transition<T>,
children: @Composable() (T) -> Unit) {
val item = transition.data
key(item.data) {
val restoredRegistry by remember(item.savedState) {
log("${item.data} Create new registry with: ${item.savedState}")
mutableStateOf(UiSavedStateRegistry(restoredValues = item.savedState, canBeSaved = { true }))
}
Providers(UiSavedStateRegistryAmbient provides restoredRegistry) {
children(item.data)
onDispose {
val saved = restoredRegistry.performSave()
log("${item.data} onDispose saved: $saved")
item.savedState = saved
}
}
}
}
Zach Klippenstein (he/him) [MOD]
09/02/2020, 1:07 PMTimo Drick
09/02/2020, 1:29 PMZach Klippenstein (he/him) [MOD]
09/12/2020, 6:04 PM