viewModel() in a Composable
Obtain and scope a ViewModel correctly.
viewModel() in a Composable is a free Jetpack Compose Academy lesson on CoddyKit — lesson 2 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.
Getting a ViewModel
Inside a Composable you don’t construct a ViewModel with new. Instead you call the viewModel() function to fetch the right instance.
val vm: CounterViewModel = viewModel()Where It Comes From
The viewModel() helper looks up the nearest lifecycle owner and returns the ViewModel scoped to it, creating one only if none exists yet.
Same Instance on Recomposition
Recomposition runs your function many times, yet viewModel() returns the same instance each time, so your state never resets unexpectedly.
Reading State From It
Once you hold the ViewModel, read its observable state and your Composable redraws whenever that state changes. The UI follows the data. 🔗
val vm: CounterViewModel = viewModel()
Text("Count: " + vm.count)Calling Its Functions
Send user actions to the ViewModel by calling its functions from event lambdas, keeping the decision-making logic out of your Composable.
Button(onClick = { vm.increment() }) {
Text("Add")
}The Import
The function comes from the lifecycle Compose artifact. Import androidx.lifecycle.viewmodel.compose.viewModel to use it.
import androidx.lifecycle.viewmodel.compose.viewModelScoping by Default
By default the ViewModel is scoped to the host Activity or navigation destination, so all Composables in that scope share one instance. 🎯
Constructor Arguments
Need to pass dependencies? Provide a factory so the framework knows how to build a ViewModel that has constructor parameters.
val vm: CartViewModel = viewModel(factory = CartFactory(repo))Hoist, Don’t Pass Down
Call viewModel() high up, near the screen root, then pass plain state and callbacks down to keep child Composables reusable.
Preview-Friendly Design
Because viewModel() needs a real owner, design children to take simple parameters so they still render in an isolated @Preview.
A Quick Pattern
The common flow is simple: obtain the ViewModel, read its state, and forward events to its functions. Three steps, every screen. ✨
Quick Check
Check how you obtain a ViewModel from a Composable.
Recap
Call viewModel() to fetch a scoped ViewModel, read its state to drive the UI, and forward user events to its functions instead of constructing it yourself.
Frequently asked questions
Is the “viewModel() in a Composable” lesson free?
Yes — the full text of “viewModel() in a Composable” 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 “viewModel() in a Composable”?
Obtain and scope a ViewModel correctly. 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 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “viewModel() in a Composable” 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 ViewModel Survives Rotation
- viewModel() in a Composable
- Modeling a UiState Data Class
- collectAsStateWithLifecycle