Is remember supposed to work correctly in LazyColumn / item on something that is always on screen or is there some subtilty here?
Tolriq
10/05/2021, 8:58 AM
Copy code
item("Key 1") {
var x by remember("Key2") { mutableStateOf(0) }
x = 3
// Do something
}
Then randomly on recomposition x is reseted to 0
a
Andrey Kulikov
10/05/2021, 10:10 AM
it is remembered while the item is visible. not visible items are disposed. however rememberSaveable will work for you
t
Tolriq
10/05/2021, 10:26 AM
I understand that it's reseted when scrolled out and wanted in that case.
But the thing is that the item is always visible, I do not scroll or anything. I just call x= a value multiple times and randomly it will reset to 0.
a
Andrey Kulikov
10/05/2021, 10:35 AM
are you sure something you pass as a param never changes? in your example you wrote it like “Key2”, but I doubt you pass a string constant in your real example? also why you change x to 3 straight away, and not pass 3 as an initial value for mutableStateOf()?
t
Tolriq
10/05/2021, 10:40 AM
This is a simplified case, but yes the keys for the test are those strings. the state is actually a list of data that I want to be kept displayed while reloading the next batch of data if it's already displayed. On the first load I have a place holder shown. All works perfectly fine 80% of the time, just sometime the remembered data is reseted to the first empty list. If I hoist the state outside of the lazycolumn it's 100% but I do not need / want to keep that data when out of screen.
a
Andrey Kulikov
10/05/2021, 10:50 AM
it could be happening when you change the total count or ordering of items displayed in LazyColumn. there is an optional key param on item(…), if you set that this will handle such use cases
t
Tolriq
10/05/2021, 10:56 AM
Nothing else change at all for those test It's a lazy column with 1 item with the static string key, containing a remember shown with a static string key and a stateflow.collectasstate that update that mutablestate. I've checked there's never a state with empty data sent. I think about a race condition or something else that makes the whole lazycolum to redraw but can't figure out.