Middle+
What ready-made components with a specific scope are available in Dagger Hilt, and how can a module be added to this component?
sobes.tech AI
Answer from AI
Dagger Hilt provides several ready-made components with specific scopes that correspond to the lifecycle of Android components:
SingletonComponent— scope for the entire application.ActivityComponent— scope for the Activity lifecycle.FragmentComponent— scope for the Fragment lifecycle.ViewModelComponent— scope for the ViewModel lifecycle.ServiceComponent— scope for the Service lifecycle.ViewComponent— scope for the View lifecycle.
To add a module to a component, create a class annotated with @Module and specify the desired component in @InstallIn. For example:
@Module
@InstallIn(ActivityComponent::class)
object MyActivityModule {
@Provides
fun provideSomeDependency(): SomeDependency {
return SomeDependency()
}
}
Thus, dependencies from this module will be available within the lifecycle of the Activity.