https://kotlinlang.org logo
#compose
Title
# compose
d

Deepak Gahlot

03/09/2021, 9:57 AM
How can we store the state of the LazyColumn. I read something about rememberSaveableStateHolder(), will that help. ?
a

Arkadii Ivanov

03/09/2021, 10:05 AM
It should be handled by the navigation library you are using, no extra work should be usually required. AFAIK
d

Deepak Gahlot

03/09/2021, 10:09 AM
I thought the same, but what i'm seeing is that if there is some change in the top bar in the Scaffold then the content which is also a Composable which has the LazyColumn it renders it self.
And LazyColumn goes back to initial scroll position
Does it suppose to happen in the first place ?
p

patrick

03/09/2021, 10:22 AM
isn’t this
val state = rememberLazyListState()
and then just pass the state to the lazy column?
d

Deepak Gahlot

03/09/2021, 10:24 AM
Yes i'm using that
t

Timo Drick

03/09/2021, 10:24 AM
If you not specify state in LazyColumn it will use
rememberLazyListState()
internally. Which also internally use rememberSaveableState... So in theory it should work out of the box.
d

Deepak Gahlot

03/09/2021, 10:26 AM
So i have a top bar in the scaffold, which bring up a bottomSheet, so that is happening that as soon as the bottom sheet comes up, the comes up the composable in the content of the scaffold is also re-rendered
Copy code
fun QuestionListRepo(questionnaireViewModel: QuestionnaireViewModel) {
    val qData by questionnaireViewModel.questionnaireData.observeAsState()
    val scaffoldState = rememberScaffoldState(rememberDrawerState(DrawerValue.Open))
    val bottomSheetScaffoldState = rememberBottomSheetScaffoldState(
        bottomSheetState = BottomSheetState(BottomSheetValue.Collapsed)
    )
    Scaffold(
        scaffoldState = scaffoldState,
        topBar = {
            QuestionnaireTopBar(bottomSheetScaffoldState, 3, questionnaireViewModel)
        },
        content = {
            when (qData) {
                is Resource.Success -> {
                    (qData as Resource.Success).data?.let {
                        GenerateUI(it, questionnaireViewModel)
                    }
                }
                is Resource.Loading -> {
                    ProgressBar()
                }
            }
        },
        bottomBar = {
            Column {
                BottomSheet(questionnaireViewModel, bottomSheetScaffoldState)
                QuestionnaireBottomBar(questionnaireViewModel)
            }
        }
    )
}
GenerateUI - is the composable which has the LazyColumn
Does the change in the bottomSheetScaffoldState, triggers a re-render in other places as well ?
t

Timo Drick

03/09/2021, 2:45 PM
A rerender should not change the state. So in theory i would expect that it works.
3 Views