Package dagger.hilt.android.components
Interface ViewModelComponent
-
public interface ViewModelComponentA Hilt component that has the lifetime of a singleViewModel.This Hilt component is the source of
HiltViewModel-annotatedViewModels used by thedagger.hilt.android.lifecycle.HiltViewModelFactory. It contains a default binding for theSavedStateHandleassociated with theViewModelthat can be used by other dependencies provided by the component.Dependencies available in the
SingletonComponentandActivityRetainedComponentare also available in this component since it is a child ofActivityRetainedComponent.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
ViewModelComponentcan be scoped using theViewModelScopedannotation. This allows for a single instance of a dependency to be provided across the dependencies of a singleHiltViewModel-annotatedViewModel.- See Also:
HiltViewModel,ViewModelScoped