[Feature Request] optional provide activity when using ContributesAndroidInjector
See original GitHub issueWhen using the ContributesAndroidInjector Annotation there’s no way to provide the activity itself. In the rare cases I need this it’s a pain to always use the “old” approach.
@Binds
@IntoMap
@ActivityKey(MainActivity::class)
internal abstract fun bindAndroidInjectorFactory(builder: MainActivitySubcomponent.Builder)
: AndroidInjector.Factory<out Activity>
@ActivityScope
@Subcomponent(modules = arrayOf(ActivityModule::class))
interface MainActivitySubcomponent : AndroidInjector<MainActivity> {
@Subcomponent.Builder
abstract class Builder : AndroidInjector.Builder<MainActivity>() {
abstract fun module(module: ActivityModule): Builder
override fun seedInstance(instance: MainActivity) {
module(ActivityModule(instance))
}
}
}
with the module:
@Module
class ActivityModule(private val activity: AppCompatActivity) {
@Provides
@ActivityScope
fun provideActivity(): AppCompatActivity = activity
}
This is a lot of boilerplater why I love the annotation you introduced in 2.11
I would love to see an additional boolean(?) flag in this annotation(may be no IN this annotation but a different one?) which is default false, so that the activity is provided (the code for the subcomponent is generated automatically)
This could be figured out by reading the type of key (@ActivityKey) in the subcomponent
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:19 (1 by maintainers)
Top Results From Across the Web
Dagger 2 ContributesAndroidInjector provide activity to module
One way to minimize the boilerplate is to make a generic ActivityModule and then create a small specific Module per Activity.
Read more >No Frames - Dagger
Performs members-injection for a concrete subtype of a core Android type (e.g., Activity or Fragment ). AndroidInjector.Builder<T> - Class in dagger.
Read more >Google I/O 2018 app — Architecture and Testing ... - Reddit
When we use a single ActivityComponent class and use them (by creating new instance) in many activities even though we are requesting dependencies...
Read more >Dagger 2 with ContributesAndroidInjector - Is it easy? - Medium
So our dagger setup is completed. Let's have a look how to inject these dependencies in our activities. That's it now we can...
Read more >MVP - fossasia
Separately, providing a new activity for the specific purpose of resetting the ... Dependencies are requested simply by using @Inject annotation e.g. in...
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 FreeTop 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
Top GitHub Comments
Maybe we could have a way to define an abstract generic module and use a type parameter for the concrete activity type. Then you can specify the module and Dagger will implement it for you:
Something like this:
You implement it once, include it in all of your @ContributesAndroidInjector declarations, and Dagger handles the rest. cc: @netdpb
@kingsleyadio not very hard with
@ContributesAndroidInjector
. Simplest way is to add a component-local module like so:Then you have AppCompatActivity bound in the scope of the MainActivity subcomponent IIRC