Package dagger.hilt.android.lifecycle
Annotation Type HiltViewModel
-
@Target(TYPE) @Retention(CLASS) public @interface HiltViewModelIdentifies aViewModelfor construction injection.The
ViewModelannotated withHiltViewModelwill be available for creation by thedagger.hilt.android.lifecycle.HiltViewModelFactoryand can be retrieved by default in anActivityorFragmentannotated withAndroidEntryPoint. TheHiltViewModelcontaining a constructor annotated withInjectwill have its dependencies defined in the constructor parameters injected by Dagger's Hilt.Example:
@HiltViewModel public class DonutViewModel extends ViewModel { @Inject public DonutViewModel(SavedStateHandle handle, RecipeRepository repository) { // ... } }@AndroidEntryPoint public class CookingActivity extends AppCompatActivity { public void onCreate(Bundle savedInstanceState) { DonutViewModel vm = new ViewModelProvider(this).get(DonutViewModel.class); } }Exactly one constructor in the
ViewModelmust be annotated withInject.Only dependencies available in the
ViewModelComponentcan be injected into theViewModel.- See Also:
ViewModelComponent