Google은 흑인 공동체를 위한 인종 간 평등을 진전시키기 위해 노력하고 있습니다. Google에서 어떤 노력을 하고 있는지 확인하세요.

ViewModelInject

@Target([AnnotationTarget.CONSTRUCTOR]) class ViewModelInject
androidx.hilt.lifecycle.ViewModelInject

Identifies a androidx.lifecycle.ViewModel's constructor for injection.

Similar to javax.inject.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 androidx.hilt.lifecycle.HiltViewModelFactory and can be retrieved by default in an Activity or Fragment annotated with dagger.hilt.android.AndroidEntryPoint.

Example:

public class DonutViewModel {
      @ViewModelInject
      public DonutViewModel(@Assisted SavedStateHandle handle, RecipeRepository repository) {
          // ...
      }
  }
  
@AndroidEntryPoint
  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 androidx.hilt.Assisted-annotated androidx.lifecycle.SavedStateHandle parameter along with any other dependency. The SavedStateHandle must not be a type param of javax.inject.Provider nor dagger.Lazy and must not be qualified.

Only dependencies available in the dagger.hilt.android.components.ActivityRetainedComponent can be injected into the ViewModel.

Summary

Public constructors

Identifies a androidx.lifecycle.ViewModel's constructor for injection.

Public constructors

<init>

ViewModelInject()

Identifies a androidx.lifecycle.ViewModel's constructor for injection.

Similar to javax.inject.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 androidx.hilt.lifecycle.HiltViewModelFactory and can be retrieved by default in an Activity or Fragment annotated with dagger.hilt.android.AndroidEntryPoint.

Example:

public class DonutViewModel {
      @ViewModelInject
      public DonutViewModel(@Assisted SavedStateHandle handle, RecipeRepository repository) {
          // ...
      }
  }
  
@AndroidEntryPoint
  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 androidx.hilt.Assisted-annotated androidx.lifecycle.SavedStateHandle parameter along with any other dependency. The SavedStateHandle must not be a type param of javax.inject.Provider nor dagger.Lazy and must not be qualified.

Only dependencies available in the dagger.hilt.android.components.ActivityRetainedComponent can be injected into the ViewModel.