@Target(value=TYPE)
@Retention(value=CLASS)
public @interface HiltViewModel
ViewModel for construction injection.
The ViewModel annotated with HiltViewModel will be available for creation by
the dagger.hilt.android.lifecycle.HiltViewModelFactory 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.
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 ViewModel must be annotated with Inject.
Only dependencies available in the ViewModelComponent
can be injected into the ViewModel.
ViewModelComponent