Mark
11/30/2023, 11:08 AMLazyListState
property to a view model to preserve the scroll position of a LazyColumn
through config changes. However, LeakCanary seems to imply this is causing a memory leak. Any ideas?Filip Wiesner
11/30/2023, 11:11 AMLazyColumn
scroll position should be automatically preserved through it's usage of rememberSaveable
🤔 So putting it in your VM should not be necessary.
Not sure about the leak tho 🤷Mark
11/30/2023, 11:25 AMrememberLazyListState
wasn’t working for me, but later realised that was because I was collecting LazyColumn
items from a flow where the initialValue
was empty and so the state became invalid (0 items). My workaround for this now is to use a dummy state corresponding to when the item state is initialValue
. Anyway, the memory leak is now gone, so thanks very much.Filip Wiesner
11/30/2023, 11:26 AMMark
11/30/2023, 11:31 AMval lazyListState = if (items == null) {
// use a dummy list state until we get a real items
rememberLazyListState()
} else {
rememberLazyListState()
}
Filip Wiesner
11/30/2023, 11:35 AMMark
11/30/2023, 11:35 AMshikasd
11/30/2023, 2:11 PMscrollToItem
whenever items change. It also should work with incorrect indices as wellMark
12/03/2023, 8:27 AMLazyListState
property in my viewmodel, which is what brought my attention to this. I’m not getting any more LC reports, so it seems fixed now.
The composable collects a flow from the ViewModel
so I think there is always going to be a moment where the initialValue
is being used. It seems the LazyListState
is updated (to item zero) by the LazyColumn
when the initial content is empty.