In the process of debugging, I noticed something that I cannot explain to myself. Here is my example
Column {
var text by remember {
mutableStateOf("")
}
TextField(value = text, onValueChange = { text = it })
Image(
painter = painterResource(id = R.drawable.mypos_world_white),
contentDescription = null
)
}
Everything is clear here, we enter the text, and we get a recomposition of both the text of the field and the picture, as painterResource is marked as unstable.
Ffter i bring the picture into a separate function the behavior changes
//call site
Column {
var text by remember {
mutableStateOf("")
}
TextField(value = text, onValueChange = { text = it })
ImageFunction()
}
@Composable
fun ImageFunction() {
Image(
painter = painterResource(id = R.drawable.mypos_world_white),
contentDescription = null
)
}
Layout Inscpector Result Now recomposition is skipped.
Shuld we bring all unstable components, like Images, out of main scope, and why this works like this?
A couple possible reasons:
ImageComposable is part of the same scope as theTextField. Since they share the same scope, any change in one will cause the other to recompose. This is because theImageComposable is marked as unstable (painterResource), so it triggers a recomposition whenever its value changes.