The Jetpack compose documentation says it can skip recomposition if all the inputs are stable and haven't changed. The definition of a type being stable goes like this...
A stable type must comply with the following contract:
- The result of equals for two instances will forever be the same for the same two instances.
- If a public property of the type changes, Composition will be notified.
- All public property types are also stable.
I cannot understand this clearly. Can someone explain to me how compose checks if a type is Stable?? I can understand if compose determines a type as stable, then it will check for equality with equals method. But how to say whether a class is stable so that we can understand and favour smart recomposition as much as possible?
I tried playing around and found the following.
This data class Student is not stable, even if I pass the same instance again, its recomposing
data class Student(
var id:Int=1,
var name:String="Anonymous")
However, the following class is treated as stable and favors smart recomposition when we pass the same instance again
This class is stable when all the public parameters are changed to val
data class Student(
val id:Int=1,
val name:String="Anonymous")
So, I can understand that all the public properties should be immutable. But to my confusion, the following class is also treated as Stable and favors smart recomposition. Why?
This MainActivityVM class is considered Stable when passing like
@Composable
fun StudentList(viewModel:MainActivityVM){
...
}
class MainActivityVM : ViewModel() {
val studentNames = mutableStateListOf<Student>()
var textInputDialogState by mutableStateOf(TextInputDialogState())
var publicMutableProperty:String = "This is mutable"
}
So, I could not figure out exactly how compose checks for stable types. Can someone help me to understand this so that I can efficiently support smart recomposition in my code?