I'm using Dagger v2.12 with dagger-android-support
with the following config:
@Singleton
@Component(
modules = arrayOf(
AndroidSupportInjectionModule::class,
AndroidBindingModule::class,
AppModule::class
)
)
interface AppComponent : AndroidInjector<App> {
@Component.Builder
abstract class Builder : AndroidInjector.Builder<App>()
}
@Module
abstract class AndroidBindingModule {
@PerActivity
@ContributesAndroidInjector(modules = arrayOf(MainModule::class))
internal abstract fun contributeMainActivityInjector(): MainActivity
}
@Module
class MainModule {
...
@Provides @PerActivity
fun providePresenter(rxLifecycle: ReactiveLifecycle, view: MainView) =
MainPresenter(rxLifecycle, view)
}
class MainActivity : BaseActivity() {
@Inject
lateinit var presenter: MainPresenter
...
}
Analysing the memory dump, I noticed that the MainPresenter
class has been created twice, one been referenced in MainActivity
and dagger.internal.DoubleCheck
(as expected) 1, but, there are a second instance referenced only in dagger.internal.DoubleCheck
2.
Why this is happening? Is this a bug, expected behaviour or some issue in my Dagger config?
Edit: Sample repository with the issue https://github.com/ismaeldivita/dagger-test-so
The problem is, that you are performing AndroidInjection.inject(this)
2 times within your activity class. That happens, because your activity is a descendant of DaggerAppCompatActivity
, which in turn also performs AndroidInjection.inject(this)
.
From the docs of DaggerAppCompatActivity
:
An
AppCompatActivity
that injects its members inonCreate(Bundle)
and can be used to injectFragments
attached to it.
After omitting AndroidInjection.inject(this)
line from your MainActivity
class you'll get the expected output in logcat:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With