0Pricing
Jetpack Compose Academy · Lesson

hiltViewModel & @HiltViewModel

Inject ViewModels into Composables.

hiltViewModel & @HiltViewModel is a free Jetpack Compose Academy lesson on CoddyKit — lesson 3 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.

ViewModels Need Dependencies Too

Your ViewModel usually depends on a repository or use case. Hilt can inject those for you, so you never build them by hand again.

Annotate the ViewModel

Mark the class with @HiltViewModel and add @Inject to its constructor. Hilt now knows how to create it with all its dependencies.

@HiltViewModel
class FeedViewModel @Inject constructor(private val repo: Repo) : ViewModel()

Inject Anything It Needs

Constructor parameters can be any type Hilt knows, like a repository, a use case, or a logger. Hilt resolves the whole chain for you.

@HiltViewModel
class FeedViewModel @Inject constructor(
    repo: Repo, logger: Logger
) : ViewModel()

Get One in Compose

Inside a Composable, call hiltViewModel() to obtain a Hilt-built ViewModel. No factory boilerplate, just one clean call.

@Composable
fun FeedScreen(vm: FeedViewModel = hiltViewModel()) { }

Add the Compose Dependency

hiltViewModel() lives in the hilt-navigation-compose artifact. Add it to your Gradle file so the function is available in your screens.

implementation("androidx.hilt:hilt-navigation-compose:1.2.0")

Default Parameter Pattern

Putting hiltViewModel() as a default parameter keeps the Composable testable. A test can still pass a fake ViewModel directly.

@Composable
fun FeedScreen(vm: FeedViewModel = hiltViewModel())

Scoped to the Owner

hiltViewModel() scopes the instance to the nearest ViewModelStoreOwner, usually the Activity or nav destination, so it survives recomposition.

One ViewModel Per Destination

Inside a NavHost, each destination gets its own ViewModel instance. Navigate away and back, and that screen receives a fresh one.

Mark the Activity Too

The host Activity must carry @AndroidEntryPoint so Hilt can deliver ViewModels into its Composables. Without it, injection fails.

@AndroidEntryPoint
class MainActivity : ComponentActivity()

Expose State, Hide Logic

The injected ViewModel exposes UI state and the screen just reads it. Your Composable stays thin while logic lives in one tested place.

val state by vm.uiState.collectAsStateWithLifecycle()

No Manual Factories

Before Hilt you wrote a ViewModelProvider.Factory for each ViewModel with arguments. Hilt removes that boilerplate completely. 🎁

Quick Check

Which pair correctly sets up a Hilt-injected ViewModel for a Composable?

Recap: Injected ViewModels

You learned to tag a ViewModel with @HiltViewModel and fetch it via hiltViewModel(), scoped to its owner with zero factory code. ✨

Frequently asked questions

Is the “hiltViewModel & @HiltViewModel” lesson free?

Yes — the full text of “hiltViewModel & @HiltViewModel” 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 “hiltViewModel & @HiltViewModel”?

Inject ViewModels into Composables. 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 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “hiltViewModel & @HiltViewModel” 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

  1. Why DI: Testable, Decoupled Code
  2. Modules, @Provides & @Binds
  3. hiltViewModel & @HiltViewModel
  4. Scopes & Component Lifetimes
← Back to Jetpack Compose Academy