Scopes & Component Lifetimes
Control how long dependencies live.
Scopes & Component Lifetimes is a free Jetpack Compose Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Jetpack Compose Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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 timeComponents 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. 🎯
Frequently asked questions
Is the “Scopes & Component Lifetimes” lesson free?
Yes — the full text of “Scopes & Component Lifetimes” is free to read here on the web, and the Jetpack Compose Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Jetpack Compose Academy course, upgrade to CoddyKit PRO.
What will I learn in “Scopes & Component Lifetimes”?
Control how long dependencies live. You practise Jetpack Compose Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Jetpack Compose Academy?
No prior experience is required. Jetpack Compose Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Scopes & Component Lifetimes” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Jetpack Compose Academy lesson?
Yes. Every Jetpack Compose Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Why DI: Testable, Decoupled Code
- Modules, @Provides & @Binds
- hiltViewModel & @HiltViewModel
- Scopes & Component Lifetimes