Does anyone have a suggestion on what’s likely goi...
# compose-android
k
Does anyone have a suggestion on what’s likely going wrong with my use of SwipeToDismiss? • User adds items to list, one by one: “Foo”, “Bar”, “Baz” • Items are saved to Room, observed in repository, etc. • Each one is wrapped in SwipeToDismiss • User swipes left on “Baz” to delete it from the list and it goes away as expected • User adds “Baz” to list again, but it doesn’t get re-added
solved 1
With some debugging it’s clear that it actually does get added, it’s just that the item is instantly seen in a dismissed state and so it immediately gets deleted. sad panda
Do I perhaps need to use
DisposableEffect
, instead of
LaunchedEffect
? Or…? It seems like everything should be created from scratch. I would think the composable has been completely destroyed and recreated, so how could it remember anything from when the tree included something with the same model (in this case “Baz”)?
p
You sound similar to this: https://stackoverflow.com/questions/76244495/problem-with-deleting-object-in-jetpack-compose Is a cache optimization somehow, compose will reuse the previous composition node if it matches the same type and position. If you don't tell Compose that your Composable function depends on a specific state. It will reuse the previous composition one, if the position in the composition tree is the same. The fix is basically using state as Keys or directly as function parameters, so compose knows the Composable should be run if the state changes. It applies to remember, Effects, List.
k
@Pablichjenkov I wondered if it was a caching sort of thing in LazyColumn. But, I don’t follow how that stackoverflow helps. Can you be more specific?
Ahh, you got me in the right direction. I changed
rememberDismissState(…)
to
remember { DismissState(…) }
and that did it. Now it will be interesting to consider what tradeoffs just happened… 🤔
solved 1
🎉 1
p
You can use a key in remember(key1 = stateID) and it will update when the key changes. Usually when using remember without a key, you are telling compose you want positional memoization. Meaning, in a recomposition, Compose will prioritize the previous value over new values