Sobes.tech
Junior — Middle

What is the role of the @Immutable annotation in Jetpack Compose and why is it used?

sobes.tech AI

Answer from AI

The @Immutable annotation in Jetpack Compose is used to denote classes or objects that are immutable. It helps the compiler and the Compose system optimize UI redraws.

When Compose knows that an object is marked as @Immutable, it assumes that the state of this object will not change, and therefore, it will not trigger unnecessary recalculations or redraws if the reference to the object remains the same.

Applying @Immutable improves UI performance and stability, especially when working with custom data types.

Example:

@Immutable
data class User(val name: String, val age: Int)

Here, Compose will consider instances of User as unchanging after creation.

What is the role of the @Immutable annotation in… - sobes.tech