Annotation Type HiltViewModel

  • @Target(TYPE)
    public @interface HiltViewModel
    Identifies a ViewModel for construction injection.

    The ViewModel annotated with HiltViewModel will be available for creation by the and can be retrieved by default in an Activity or Fragment annotated with AndroidEntryPoint. The HiltViewModel containing a constructor annotated with Inject will have its dependencies defined in the constructor parameters injected by Dagger's Hilt.


     public class DonutViewModel extends ViewModel {
         public DonutViewModel(SavedStateHandle handle, RecipeRepository repository) {
             // ...
     public class CookingActivity extends AppCompatActivity {
         public void onCreate(Bundle savedInstanceState) {
             DonutViewModel vm = new ViewModelProvider(this).get(DonutViewModel.class);

    Exactly one constructor in the ViewModel must be annotated with Inject.

    Only dependencies available in the ViewModelComponent can be injected into the ViewModel.

    See Also: