I have a `LazyColumn` with multiple `item`/`ìtems`...
# compose
n
I have a
LazyColumn
with multiple `item`/`ìtems`. When I navigate to a “detail” screen, and then go back, the scroll position is lost. Is this the right bug to follow? https://issuetracker.google.com/issues/179397301
a
what exactly do you display? do you have items which are loaded lazily like from Flow or Paging?
n
yep
Particularly from
stateFlow.collectAsState()
Here is some pseudo-code…
Copy code
val myState by viewModel.myStateFlow.collectAsState(null)
LazyColumn {
    item(ID_H1) { Header1() }
    items(
        myState.list1,
        key = { it.id }
    ) { ItemType1(it) }
    item(ID_H2) { Header2() }
    items(
        myState.list2,
        key = { it.id }
    ) { ItemType2(it) }
    item(ID_H3) { Header3() }
    items(
        myState.list3,
        key = { it.id }
    ) { ItemType3(it) }
}
as you can see, I’m also assigning the id for all the item/items
a
yeah, it is the same issue as in the mentioned bug. you can work around it by not emitting LazyColumn while you don’t have the data yet, like if (myState != null) { LazyColumn() } else { some other ui }
👍 1
n
Thanks. I’ll try that 😉
Actually, I don’t now if I can because these three sections comes from 3 different flows, then I would like to display each progress separately
a
you basically need to defer the state restoration till the moment the elements are ready