Jetpack Compose Academy · Lektion

Scopes und Lebenszyklen von Komponenten

Steuern Sie, wie lange Dependencies bestehen bleiben.

Lektion 4 von 413 Schritte

Scopes und Lebenszyklen von Komponenten ist eine kostenlose Jetpack Compose Academy-Lektion auf CoddyKit. Dies ist Lektion 4 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.

What a Scope Controls

A scope decides how long Hilt keeps a single instance alive and where it gets shared. It is about lifetime, not just creation.

Unscoped by Default

Without a scope annotation, Hilt builds a new instance every time it is requested. That is fine for cheap, stateless objects.

@Provides
fun provideMapper(): Mapper = Mapper() // fresh each time

Components Mirror Android

Hilt provides a set of components that match Android lifetimes, from the whole app down to a single view. Each owns its own instances.

App-Wide With Singleton

SingletonComponent lives as long as the app. Bindings here with @Singleton are created once and shared across every screen.

@InstallIn(SingletonComponent::class)

Per-Activity Lifetime

ActivityComponent follows one Activity. Use @ActivityScoped to share an instance across that Activity and recreate it on a new one.

@ActivityScoped
class FormState @Inject constructor()

ViewModel Scope

ViewModelComponent matches a ViewModel's life. With @ViewModelScoped a dependency is shared only within that one ViewModel.

@ViewModelScoped
class SessionCache @Inject constructor()

Scope Equals Annotation Plus Component

Each scope annotation pairs with one component. @Singleton goes with SingletonComponent, @ActivityScoped with ActivityComponent, and so on.

Lifetimes Form a Hierarchy

Components nest: an Activity lives inside the app, a Fragment inside the Activity. A child can use anything its parents provide.

Scope With Care

Scoping costs memory, because the instance is held until its component dies. Only scope objects whose state genuinely needs to be shared.

Avoid Leaking the Wider Scope

Never inject a short-lived object, like an Activity, into a Singleton. The longer-lived holder would keep it alive and leak memory. ⚠️

Picking the Right Scope

Match the scope to the data's life: app config in Singleton, screen state in ViewModelScoped. The right fit prevents leaks and waste.

Quick Check

You want one shared instance for the entire app lifetime. Which scope fits?

Recap: Lifetimes Under Control

You learned that scopes tie an instance's lifetime to a component, from Singleton down to ViewModel, so dependencies live exactly as long as needed. 🎯

Kostenlos starten

Lerne Kotlin mit einem KI-Tutor — kostenlos

Schreibe und führe echten Code in deinem Browser aus, bekomme sofortige Hilfe von einem 24/7 KI-Tutor und setze dein Lernen im Web oder in der App fort.

Kurse
30
Lektionen
120

Häufig gestellte Fragen

Ist die Lektion „Scopes und Lebenszyklen von Komponenten“ kostenlos?

Ja — der vollständige Text von „Scopes und Lebenszyklen von Komponenten“ 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 „Scopes und Lebenszyklen von Komponenten“?

Steuern Sie, wie lange Dependencies bestehen bleiben. 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 4 von 4.

Wie lange dauert die Lektion „Scopes und Lebenszyklen von Komponenten“?

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

  1. Warum DI: testbarer, entkoppelter Code
  2. Module, @Provides und @Binds
  3. hiltViewModel und @HiltViewModel
  4. Scopes und Lebenszyklen von Komponenten
← Zurück zu Jetpack Compose Academy