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 PMTimo Drick
09/02/2020, 12:13 PMTimo Drick
09/02/2020, 12:13 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 PMTimo Drick
09/02/2020, 12:18 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@5f7ad68Timo Drick
09/02/2020, 12:20 PMTimo Drick
09/02/2020, 12:21 PMZach 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 PMTimo Drick
09/02/2020, 12:25 PMcurrentComposer.currentCompoundKeyHash
Is not the same when the same composable is created againTimo Drick
09/02/2020, 12:26 PMZach Klippenstein (he/him) [MOD]
09/02/2020, 12:28 PMkey composables.Timo Drick
09/02/2020, 12:29 PMTimo 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)
}Timo Drick
09/02/2020, 12:33 PMZach Klippenstein (he/him) [MOD]
09/02/2020, 12:35 PMtransition() do?Timo Drick
09/02/2020, 12:38 PMTimo Drick
09/02/2020, 12:40 PMTimo Drick
09/02/2020, 12:41 PMZach Klippenstein (he/him) [MOD]
09/02/2020, 12:52 PMTimo Drick
09/02/2020, 12:58 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
}
}
}
}Timo Drick
09/02/2020, 12:58 PMTimo Drick
09/02/2020, 12:59 PMTimo Drick
09/02/2020, 1:00 PMTimo Drick
09/02/2020, 1:02 PMTimo Drick
09/02/2020, 1:02 PMTimo Drick
09/02/2020, 1:03 PMZach Klippenstein (he/him) [MOD]
09/02/2020, 1:07 PMZach Klippenstein (he/him) [MOD]
09/02/2020, 1:07 PMTimo Drick
09/02/2020, 1:29 PMTimo Drick
09/02/2020, 1:30 PMZach Klippenstein (he/him) [MOD]
09/12/2020, 6:04 PM