Junior — Middle
Can you explain the use of the @Stable annotation in Jetpack Compose, and why it is applied?
sobes.tech AI
Answer from AI
The @Stable annotation in Jetpack Compose is used to indicate that an object or class is stable from Compose's perspective — meaning its state does not change or changes predictably.
This helps the Compose system optimize UI redraws. If an object is marked as @Stable, Compose assumes that its properties will not change without notification, and there will be no unnecessary recompositions if the reference to the object does not change.
Example of usage:
@Stable
class User(val name: String, val age: Int)
If the User object does not change after creation, Compose will not recompute the UI depending on this object unnecessarily.
Thus, @Stable helps improve performance by reducing unnecessary UI updates.