Google 致力于为黑人社区推动种族平等。查看具体举措


public abstract @interface ViewModelInject
implements Annotation


This @interface is deprecated.
Use HiltViewModel.

Identifies a ViewModel's constructor for injection.

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


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

Only one constructor in the ViewModel must be annotated with ViewModelInject. The constructor can optionally define a Assisted-annotated SavedStateHandle parameter along with any other dependency. The SavedStateHandle must not be a type param of Provider nor Lazy and must not be qualified.

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


Inherited methods