Package dagger.hilt.android.components
Interface ViewModelComponent
-
public interface ViewModelComponent
A Hilt component that has the lifetime of a singleViewModel
.This Hilt component is the source of
HiltViewModel
-annotatedViewModel
s used by thedagger.hilt.android.lifecycle.HiltViewModelFactory
. It contains a default binding for theSavedStateHandle
associated with theViewModel
that can be used by other dependencies provided by the component.Dependencies available in the
SingletonComponent
andActivityRetainedComponent
are 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
ViewModelComponent
can be scoped using theViewModelScoped
annotation. This allows for a single instance of a dependency to be provided across the dependencies of a singleHiltViewModel
-annotatedViewModel
.- See Also:
HiltViewModel
,ViewModelScoped