Package dagger.hilt.android.components
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 ViewModel
s
used by the
. It contains a default
binding for the invalid @link
dagger.hilt.android.lifecycle.HiltViewModelFactory
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: