question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Feature Request] optional provide activity when using ContributesAndroidInjector

See original GitHub issue

When 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:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:19 (1 by maintainers)

github_iconTop GitHub Comments

11reactions
ronshapirocommented, Apr 4, 2018

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:

@Module
@ContributesAndroidInjector.GenericModule // with a better name
interface ActivityModule<T extends Activity> {
  @Binds AppCompatActivity bind(T activity);
  @Binds @MySpecialQualifier Context context(T t);
}

You implement it once, include it in all of your @ContributesAndroidInjector declarations, and Dagger handles the rest. cc: @netdpb

4reactions
liminalcommented, Mar 12, 2018

@kingsleyadio not very hard with @ContributesAndroidInjector. Simplest way is to add a component-local module like so:

@Module
abstract class ApplicationModule {

    @ContributesAndroidInjector(modules = [MainActivityModule::class])
    abstract fun provideActivity(): MainActivity
}

@Module
abstract class MainActivityModule {
    @Binds
    abstract fun provideActivity(mainActivity: MainActivity): AppCompatActivity
}

Then you have AppCompatActivity bound in the scope of the MainActivity subcomponent IIRC

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found