Android Academy · Lesson

Injecting into ViewModels

Use @HiltViewModel for clean injection.

Lesson 4 of 413 steps

Injecting into ViewModels is a free Android Academy lesson on CoddyKit. This is lesson 4 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Android Academy learning path, and your progress syncs across the web and the CoddyKit app. The Android Academy course includes 4 lessons in total.

Injecting ViewModels with Hilt

ViewModels are special: the framework instantiates them via a factory. Hilt integrates with this through the @HiltViewModel annotation.

Annotating the ViewModel

Mark the ViewModel with @HiltViewModel and annotate its constructor with @Inject.

import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject

@HiltViewModel
class UserViewModel @Inject constructor(
    private val repo: UserRepository
) : ViewModel()

What Hilt Provides

Hilt looks up each constructor parameter in the dependency graph. As long as UserRepository is bound (via a module or constructor injection), Hilt supplies it automatically.

Retrieving It in Compose

Use hiltViewModel() inside a Composable to obtain a Hilt-provided ViewModel.

import androidx.hilt.navigation.compose.hiltViewModel

@Composable
fun UserScreen(
    vm: UserViewModel = hiltViewModel()
) {
    val state by vm.uiState
        .collectAsStateWithLifecycle()
}

The Host Must Be an Entry Point

For hiltViewModel() to work, the hosting Activity must be annotated @AndroidEntryPoint. That connects Compose to the Hilt graph.

@AndroidEntryPoint
class MainActivity : ComponentActivity()

Injecting Other Dependencies

You can inject anything in the graph: repositories, use cases, even the Room database provided by a module.

@HiltViewModel
class HomeViewModel @Inject constructor(
    private val getUsers: GetUsersUseCase,
    private val analytics: Analytics
) : ViewModel()

Injecting Application Context

Need a context? Inject the qualified application context with @ApplicationContext.

@HiltViewModel
class PrefsViewModel @Inject constructor(
    @ApplicationContext private val context: Context
) : ViewModel()

SavedStateHandle

Hilt automatically provides SavedStateHandle, which carries navigation arguments and survives process death.

@HiltViewModel
class DetailViewModel @Inject constructor(
    handle: SavedStateHandle
) : ViewModel() {
    val id: Int = handle.get<Int>("id") ?: 0
}

The Full Wiring

End to end: Module provides the repository → Hilt injects it into the ViewModel → Compose gets the ViewModel via hiltViewModel(). No manual factories anywhere.

No Manual Factory Code

Without Hilt you would write a custom ViewModelProvider.Factory for every ViewModel that needs constructor arguments. @HiltViewModel eliminates all of that.

Putting the Track Together

You now have the full modern stack: ViewModel + lifecycle-aware state, Room persistence, coroutines and Flow, all wired with Hilt DI.

Quick Check

Test your understanding of injecting ViewModels.

Recap

You learned:

  • @HiltViewModel + @Inject constructor lets Hilt build ViewModels.
  • Obtain them in Compose with hiltViewModel() inside an @AndroidEntryPoint host.
  • Inject repositories, @ApplicationContext, and SavedStateHandle.
  • No manual factories needed.
Free to start

Learn Kotlin with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
36
Lessons
152

Frequently Asked Questions

Is the “Injecting into ViewModels” lesson free?

Yes — any 3 lessons of the Android Academy learning path, including “Injecting into ViewModels”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Android Academy course includes 4 lessons in total.

What will I learn in “Injecting into ViewModels”?

Use @HiltViewModel for clean injection. You practise Android 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 Android Academy?

No prior experience is required. Android Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.

How long does the “Injecting into ViewModels” 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 Android Academy lesson?

Yes. Every Android 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 Dependency Injection?
  2. Setting Up Hilt
  3. Modules and Providers
  4. Injecting into ViewModels
← Back to Android Academy