Daniele B
10/05/2020, 1:14 PMLazyColumnFor
issue about remembering the scrolling position was fixed. But I just checked and it’s not, at least not automatically.
My use case is very simple:
a list of elements; click on one element to display the detail page; going back to the list; the position is reset to the top, and not kept
So, do we need to explicitly set that it should be remembered? And How?
I mean that would be strange, as in the normal Android view system, the list scroll position is remebered when you go back to the list screen.Andrey Kulikov
10/05/2020, 1:24 PMLazyColumnFor
, if you navigate between screens with just if statements their state is lost when you switch the condition. it will be solved with a navigation library integration in the futureDaniele B
10/05/2020, 2:03 PMScaffold(
topBar = {
MasterTopBar(state = state)
},
bodyContent = { paddingValues -> LazyColumnFor(
contentPadding = paddingValues,
items = state.countriesListData.list
) { row ->
MasterListRow(row, model)
}
},
bottomBar = {
MasterBottomBar(model = model, state = state)
}
)
Zach Klippenstein (he/him) [MOD]
10/05/2020, 2:54 PMDaniele B
10/05/2020, 3:54 PMZach Klippenstein (he/him) [MOD]
10/05/2020, 4:16 PMDaniele B
10/05/2020, 4:18 PM@Composable
fun ScreenRouter(coreModel: CoreViewModel) {
val state by coreModel.stateFlow.collectAsState()
if (state.detailState.selectedCountry == null) {
MasterView(model = coreModel, state = state.masterState)
} else {
DetailView(model = coreModel, state = state.detailState)
}
}
detailState.selectedCountry
to nullZach Klippenstein (he/him) [MOD]
10/05/2020, 4:47 PMDaniele B
10/05/2020, 4:51 PMZach Klippenstein (he/him) [MOD]
10/05/2020, 5:12 PMUiSavedStateRegistry
yourself, although that’s not trivial. There are a couple alternative navigation libraries at this link which support it already: https://foso.github.io/Jetpack-Compose-Playground/compose_projects/#librariesDaniele B
10/05/2020, 5:23 PMLazyListState
you were mentioning a few days ago? I was thinking it allowed to save the list state into an object, combined with a function to reestablish the scroll position?Zach Klippenstein (he/him) [MOD]
10/05/2020, 5:30 PMrememberLazyListState()
already does what is needed to support saving and restoring itself – it uses `rememberSavedInstanceState`: https://cs.android.com/androidx/platform/frameworks/support/+/androidx-master-dev:compose/foundation/foundation/src/commonMain/kotlin/androidx/compose/foundation/lazy/LazyListState.kt;l=71?q=rememberLazyListState&sq=
However, rememberSavedInstanceState
requires a UiSavedStateRegistry
to be provided in the UiSavedStateRegistryAmbient
, and for the state to actually be saved before it skips/gets removed from the composition. When you’re skipping it by just using a simple if
, that’s not happening. These other navigation libraries all detect that a screen is about to be skipped, and do all the work to ask the registry to save stuff before skipping. That’s why the scroll position isn’t being saved in your case.Daniele B
10/05/2020, 5:55 PMSe7eN
10/06/2020, 4:14 PMcompose-router
doesn't save the state for me thoZach Klippenstein (he/him) [MOD]
10/06/2020, 4:15 PMcompose-backstack
definitely does, but i need to update the compose version and do a release