Johnny
10/15/2021, 9:05 AMStrings
from LiveData
? When the items are sorted it should not cause recomposition. But seems like observeAsState
forces recomposition every time. Even though I’m using key
val value by viewModel.data.observeAsState()
LazyColumn {
value?.forEach {
item(key = it) {
Text( text = "$it!")
}
}
}
Filip Wiesner
10/15/2021, 9:12 AMEvery time there would be new value posted into theIf you don't want your Composable to recompose, make sure the LiveData does not emit new value. I haven't touched LiveData for a while so I don't know how it behaves. But in the end you shouldn't care about when your component recomposes in most cases. Why do you want to prevent the recomposition?the returnedLiveData
will be updated causing recomposition of everyState
usage.State.value
divid3d
10/15/2021, 9:19 AMdistinctUntilChanged
Johnny
10/15/2021, 9:19 AMMovieOverView
example here: https://developer.android.com/jetpack/compose/lifecycle#add-info-smart-recomposition
Even if we add an item to the beginning of the list (shifting all previous items down), because we set a key
which maps to the ID of the item, the existing instance should be able to be used. I.e. no recompositionJohnny
10/15/2021, 9:24 AM// list emitted
banana
apple
orange
// list emitted 2nd time
apple
banana
orange
Filip Wiesner
10/15/2021, 9:32 AMkey
is not really meant to prevent recomposition (even thought it might have that effect sometimes) but rather to keep the same composition instance - e.g. keeping correct instances inside remember
.Johnny
10/15/2021, 9:54 AMCsaba Kozák
10/15/2021, 11:06 AM