Add custom module having constructor before AndroidInjection.inject()
See original GitHub issueI’m testing about dagger.android
. I wonder how to add custom module having constructor.
In usual, when we use AndroidInjection.inject(activity)
, It declares all module has no constructor, right?
example
@Module class ApiModule {
@Provides fun retrofit(): Retrofit = Retrofit.Builder().build()
}
But in my case, I have some module having constructor
example
@Module class SomeModule(private val a : InterfaceA, private val b : InterfaceB) {
@Provides fun a() : InterfaceA = a
@Provides fun b() : InterfaceB = b
}
InterfaceA
and InterfaceB
need to be declared in Activity
or Fragment
So now it has a big problem when doing injection.
class Presenter @Inject internal constructor(private val a : InterfaceA, private val b : InterfaceB) {
// do something....
}
// before dagger.android
class AppActivity : Activity {
@Inject lateinit var presenter : Presenter
override fun onCreate(bundle: Bundle?) {
super.onCreate(bundle)
DaggerAppActivityComponent.builder()
.SomeModule(SomeModule(a = xxx, b = yyy)
.inject(this)
}
}
// after dagger.android
class AppActivity : DaggerActivity {
@Inject lateinit var presenter : Presenter
override fun onCreate(bundle: Bundle?) {
super.onCreate(bundle)
}
}
My question is this :
When we need module having custom constructor but its arguments cannot produce in dagger-graph. If we use AndroidInjection.inject()
, then how do we add that module?
Issue Analytics
- State:
- Created 7 years ago
- Comments:5
Top Results From Across the Web
Using Dagger 2 for dependency injection in Android - Tutorial
@Component : enable selected modules and used for performing dependency injection ... If you annotate a constructor with @Inject , Dagger 2 can...
Read more >Using Dagger in Android apps
Use constructor injection with @Inject to add types to the Dagger graph whenever it's possible. When it's not: Use @Binds to tell Dagger...
Read more >Using Dagger 2 to inject into service - android - Stack Overflow
I wrote the code from the top of my head, so there could be a typo or two. You do it just the...
Read more >How to implement Dependency Injection in your app with ...
Instead of creating and configuring a new MyLibrary class object inside your class MyAppClass , you just pass or inject it into the...
Read more >Dagger 2 – Rules of Engagement - Android Stuff
3. Components can be called in 2 ways. You will have seen calls to component. · 4. Use constructor injection · 5. Modules...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
There currently isn’t a way to do this. How do you know what values to pass to the module? Are they a function of the Activity, or the Activity’s Intent? If so, then you can use the activity binding do retrieve them in a provides method
Can you add a module like this to your subcomponent: