I'm learning dagger and I fount this article that explains how to use android.dagger
everything is clear for me except with custom created scopes. Previously I saw a lot of tutorials where custom scope is created to create dependencies to specific situations (e.g Logged In scope). But that tutorial showed my other approach. Here's my example:
I have a class that should be generated only for MainActivity
(and MasterActivity
) but not for LoginActivity
class SecretKey(
val token: String,
val userId: Long
)
So here's the module
@Module
class MainActivityModule {
@Provides
fun provideSecretKey(preference: SharedPreferences): SecretKey {
return SecretKey(
"jwtToken",
465465
)
}
}
and ActivitiesBindingModule
@Module
abstract class ActivitiesBindingModule {
@ContributesAndroidInjector(modules = [MainActivityModule::class])
abstract fun mainActivity(): MainActivity
@ContributesAndroidInjector(modules = [LoginActivityModule::class])
abstract fun loginactivity(): LoginActivity
// MasterActivity will see everything from MainActivityModule and LoginActivityModule
@ContributesAndroidInjector(modules = [MainActivityModule::class, LoginActivityModule::class])
abstract fun masterActivity(): MasterActivity
}
So as I understood only in MainActivity
and MasterActivity
I will be able to inject SecretKey
class because of the ContributesAndroidInjector
module. So SecretKey
scope is within MainActivity
and MasterActivity
. So why we still are able to create custom scopes with Scope
annotation? Is this alternative?