0Pricing
Android Academy · Lesson

Injecting into ViewModels

Use @HiltViewModel for clean injection.

Injecting into ViewModels is a free Android 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 Android Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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.

Frequently asked questions

Is the “Injecting into ViewModels” lesson free?

Yes — the full text of “Injecting into ViewModels” is free to read here on the web, and the Android 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 Android Academy course, upgrade to CoddyKit PRO.

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; 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 “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