julioromano
02/04/2021, 2:40 PMStateRestorationPolicy.PREVENT_WHEN_EMPTY
for LazyColumn
?
LazyColumn
doesn’t currently restore the scroll position if the list’s data is fed using something like val myList by aFlow.collectAsState(emptyList())
.
For example during configuration changes (like rotation, dark/light theme switch, etc.) the flow will be resubscribed to and so aFlow
will temporarily emit emptyList()
causing the saved scroll position to be discarded.
This is a workaround I found but I was hoping it could be handled better:
if (myList.isNotEmpty()) {
LazyColumn() {
// .....
}
}
Ian Lake
02/04/2021, 3:33 PMshareIn
inside a ViewModel, thus ensuring that your last data is immediately available after a config change, skipping the whole empty list problem entirelyjulioromano
02/04/2021, 3:42 PMViewModel
only helps for configuration changes but not for system initiated process death (SIPD).
I was looking for a solution that could encompass both.
Throwing in a ViewModel to optimize the configuration change part is always a nice to have and can be done with little effort.
In other words: if one solves the SIPD side, the configuration change side gets taken care of implicitly.Andrey Kulikov
02/04/2021, 8:12 PMjulioromano
02/04/2021, 9:17 PMAlbert Chang
02/05/2021, 12:50 AMshareIn
works because when using collectAsState()
on flows other than `StateFlow`s you always have to specify an initial value and that initial value will always be used even if the flow emits immediately.
In my app I'm using collectAsState()
on a flow produced by transforming the value of StateFlow
in a ViewModel and I'm facing the same problem. I have tried shareIn
and it didn't work.Ian Lake
02/05/2021, 12:50 AMstateIn
then