Überspringen, @Stable und @Immutable
Kennzeichnen Sie Typen, damit Compose Arbeit überspringen kann.
Überspringen, @Stable und @Immutable ist eine kostenlose Jetpack Compose Academy-Lektion auf CoddyKit. Dies ist Lektion 2 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Jetpack Compose Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Jetpack Compose Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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. ✅
Häufig gestellte Fragen
Ist die Lektion „Überspringen, @Stable und @Immutable“ kostenlos?
Ja — der vollständige Text von „Überspringen, @Stable und @Immutable“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Jetpack Compose Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Jetpack Compose Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Überspringen, @Stable und @Immutable“?
Kennzeichnen Sie Typen, damit Compose Arbeit überspringen kann. Du übst Jetpack Compose Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Jetpack Compose Academy zu starten?
Keine Vorkenntnisse erforderlich. Jetpack Compose Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 2 von 4.
Wie lange dauert die Lektion „Überspringen, @Stable und @Immutable“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Jetpack Compose Academy-Lektion Code schreiben und ausführen?
Ja. Jede Jetpack Compose Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Stabile und instabile Parameter
- Überspringen, @Stable und @Immutable
- State-Lesevorgänge verzögern
- Layout Inspector und Recomposition-Zähler