Interface ViewModelComponent


  • public interface ViewModelComponent
    A Hilt component that has the lifetime of a single ViewModel.

    This Hilt component is the source of HiltViewModel-annotated ViewModels used by the dagger.hilt.android.lifecycle.HiltViewModelFactory. It contains a default binding for the SavedStateHandle associated with the ViewModel that can be used by other dependencies provided by the component.

    Dependencies available in the SingletonComponent and ActivityRetainedComponent are also available in this component since it is a child of ActivityRetainedComponent.

    Example usage:

     @Module
     @InstallIn(ViewModelComponent.class)
     public final class ViewModelMovieModule {
         @Provides
         public static MovieRepository provideRepo(SavedStateHandle handle) {
             return new MovieRepository(handle.getString("movie-id"));
         }
     }
     

    Dependencies in the ViewModelComponent can be scoped using the ViewModelScoped annotation. This allows for a single instance of a dependency to be provided across the dependencies of a single HiltViewModel-annotated ViewModel.

    See Also:
    HiltViewModel, ViewModelScoped