Rick Regan
12/03/2021, 3:49 PM.verticalScroll(scrollState)
.
• val scrollState = rememberScrollState()
does not survive a configuration change.
• val scrollState = rememberSaveable { ScrollState(0) }
gets an exception "ScrollState@fb46372 cannot be saved using the current SaveableStateRegistry".
• val scrollState = ScrollState(0)
globally outside of any Composable works, but this is putting the scroll state outside the scope of the Composable. Is it OK to do that? Is there another way?Ian Lake
12/03/2021, 4:40 PMrememberScrollState
uses rememberSaveable
internally, so it certainly does save and restore its state after a config change: https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:compose/foundation/foundation/src/commonMain/kotlin/androidx/compose/foundation/Scroll.kt;l=81Rick Regan
12/03/2021, 4:42 PMIan Lake
12/03/2021, 4:43 PMRick Regan
12/03/2021, 4:46 PMText
that I've scrolled down into. When I rotate, the text is still there and in its proper scroll position when I use the third option.Rick Regan
12/03/2021, 5:18 PMrememberScrollState
does work in a small example, but not in my app. I'll keep looking into it...Rick Regan
12/03/2021, 6:22 PMif (maxWidth < 500.dp) {
MyComposable()
} else {
MyComposable()
}
So each one gets its own fresh ScrollState
. (The text they display is in my app state.) To fix it, I have to hoist rememberScrollState
. If I put it in that top-level composable then I have to pass it down through many layers of composables (not shown). That makes option 3 above (val scrollState = ScrollState(0)
in my app state) more appealing. But is that bad practice?Ian Lake
12/03/2021, 6:38 PMIan Lake
12/03/2021, 6:38 PMRick Regan
12/03/2021, 7:50 PMrememberSaveable
. I have the text as text by mutableStateOf()
in my "view model" (not actually ViewModel); so looking ahead: is rememberSaveable(text) { text }
(at an appropriate-level composable) the way to do it so that it keeps updating the "save"?Ian Lake
12/03/2021, 8:26 PMrememberSaveable
Rick Regan
12/03/2021, 8:27 PM