0Pricing
Jetpack Compose Academy · 课时

跳过、@Stable 与 @Immutable

标记类型,让 Compose 能够跳过工作。

跳过、@Stable 与 @Immutable 是 CoddyKit 上的免费 Jetpack Compose Academy 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Jetpack Compose Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Jetpack Compose Academy 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

When You Need Annotations

Sometimes a type is truly safe but Compose cannot prove it. Annotations let you promise stability so skipping kicks back in.

The @Immutable Promise

@Immutable tells Compose that once an instance is built, none of its properties will ever change. It is the strongest stability guarantee.

@Immutable
data class Theme(val primary: Long, val radius: Int)

When to Use @Immutable

Reach for @Immutable on config-like data: themes, styles, and value objects you create once and never mutate after construction.

The @Stable Promise

@Stable is gentler: properties may change, but when they do, Compose will be notified through Compose state. equals() must stay consistent.

@Stable for Observable Types

Use @Stable on classes whose mutable fields are backed by mutableStateOf, so Compose still tracks every change correctly.

@Stable
class FormState { var name by mutableStateOf("") }

Break the Promise, Break Compose

These annotations are a contract. If you mark a type stable but mutate it silently, the UI shows stale data and bugs creep in.

Immutable Collection Helpers

Instead of List, pass ImmutableList from kotlinx.collections.immutable. Compose treats it as stable, so list Composables can skip.

fun Tags(items: ImmutableList<String>) { }

Functions Stay Stable

Lambda parameters are stable already, but remember a lambda capturing changing values is a new instance each recomposition.

Mark the Whole Class Once

One annotation on the class covers every use of that type as a parameter. You do not annotate at each call site.

Annotations Are Last Resort

Prefer real immutability with val first. Only annotate when the compiler genuinely cannot infer stability on its own.

Verify, Then Trust

After annotating, check that the Composable actually skips. A wrong promise is worse than no promise at all.

Quick Check

Pick the right annotation.

Recap: Promise Wisely

Use @Immutable for never-changing data and @Stable for state-backed mutables. Honor the contract or the UI lies. ✅

常见问题解答

「跳过、@Stable 与 @Immutable」课时是免费的吗?

是的 — 「跳过、@Stable 与 @Immutable」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Jetpack Compose Academy 课程的其余内容,请升级到 CoddyKit PRO。 Jetpack Compose Academy 课程共包含 4 节课。

「跳过、@Stable 与 @Immutable」这节课中我会学到什么?

标记类型,让 Compose 能够跳过工作。 你通过在浏览器中直接运行的动手代码来练习 Jetpack Compose Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Jetpack Compose Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Jetpack Compose Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。

「跳过、@Stable 与 @Immutable」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Jetpack Compose Academy 课中编写并运行代码吗?

能。每节 Jetpack Compose Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 稳定与不稳定参数
  2. 跳过、@Stable 与 @Immutable
  3. 延迟读取状态
  4. 布局检查器与重新组合次数
← 返回 Jetpack Compose Academy