What do the @Stable and @Immutable annotations mean in Jetpack Compose?

Viewed 3556

While studying through the Jetpack Compose sample project, I saw @Stable and @Immutable annotations. I've been looking through the Android documentation and GitHub about those annotations, but I don't understand.

From what I understand, if use @Immutable, even if the state is changed, recomposition should not occur. However, as a result of the test, recomposition proceeds.

What exactly do @Stable and @Immutable annotations do in Jetpack Compose?

1 Answers

The compiler treats both identically but

  • using @Immutable is a promise that the value will never change.
  • using @Stable is a promise that the value is observable and if it does change listeners are notified.
Related