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

julioromano

11/03/2023, 4:03 PM
Is there any way to
remember
some data inside a
LazyList
item so that it would be retained in memory when the item scrolls in and out of the list?
k

Konstantin Tskhovrebov

11/03/2023, 4:20 PM
to make it outside the List?
f

Francesc

11/03/2023, 4:29 PM
remember
persists data across recompositions; once your item is no longer in the composition, the
remember
will be discarded. You'd have to remember this at the list scope. What's your use case?
s

shikasd

11/03/2023, 4:37 PM
rememberSaveable
kinda does this, but you probably want to move it outside of lazy list item
j

julioromano

11/03/2023, 5:12 PM
Yeah the
rememberSaveable
part was quite counterintuitive to me but it's indeed true. Though I'd also wish to actually preserve instances of object, not just their values.
I get that moving it outside the list is the best way. But is there some perhaps low level API that allows to do this from within a list item's composable? (e.g. something that allows to save arbitrary stuff to the slot table directly, or similar...)
(so that I can build a nicer looking API with it)
s

shikasd

11/03/2023, 5:14 PM
well
remember
does save stuff to slot table directly 🙂
your problem is that slot table is getting cleared when item is reused, so it is no good you can use view model or something similar (maybe try to just throw a map into
CompositionLocal
)
j

julioromano

11/16/2023, 12:23 PM
rememberSaveable
kinda does this, but you probably want to move it outside of lazy list item
I've experimented a bit with
rememberSaveable
in items of a
LazyColumn
. It behaves as expected initially but if the items include anything that uses a
SubcomposeLayout
no state will be kept even with
rememberSaveable
. Is this expected?
s

shikasd

11/16/2023, 12:33 PM
No, not expected, it should work in this case. Please file a bug with a repro
2 Views