Compose considers types to be either stable or unstable. A type is stable if it is immutable, or if it is possible for Compose to know whether its value has changed between recompositions. A type is unstable if Compose can't know whether its value has changed between recompositions.
Compose uses the stability of a composable's parameters to determine whether it can skip the composable during recomposition:
- Stable parameters: If a composable has stable parameters that have not changed, Compose skips it.
- Unstable parameters: If a composable has unstable parameters, Compose always recomposes it when it recomposes the component's parent.
If your app includes many unnecessarily unstable components that Compose always recomposes, you might observe performance issues and other problems.
This document details how you can increase the stability of your app to improve performance and overall user experience.
Immutable objects
The following snippets demonstrates the general principles behind stability and recomposition.
The Contact class is an immutable data class. This is because all its
parameters are primitives defined with the val keyword. Once you create an
instance of Contact, you cannot change the value of the object's properties.
If you attempted to do so, you would create a new object.
data class Contact(val name: String, val number: String)
The ContactRow composable has a parameter of type Contact.
@Composable
fun ContactRow(contact: Contact, modifier: