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.

How can we add a module to Subcomponent.Builder in version 2.10-rc2?

See original GitHub issue

With the new functionality, we can inject Activity and Fragment using a single AndroidInjection.inject(this) call. But how can we pass a module to Subcomponent’s builder?

For example, Activity’s subcomponent has a module which needs this Activity in the constructor.

@ActivityScope
@Subcomponent(modules = SplashActivitySubcomponent.SplashActivityModule.class)
public interface SplashActivitySubcomponent extends AndroidInjector<SplashActivity> {
    @Subcomponent.Builder
    abstract class Builder extends AndroidInjector.Builder<SplashActivity> {
    }

    @Module
    abstract class SplashActivityModule extends ActivityModule<SplashActivity> {
        SplashActivityModule(SplashActivity activity) {
            super(activity);
        }

        @ActivityScope
        @Provides
        SplashContract.View provideView() {
            return activity;
        }

        @ActivityScope
        @Provides
        SplashContract.Presenter providePresenter(SplashPresenter presenter) {
            return presenter;
        }
    }
}

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7

github_iconTop GitHub Comments

4reactions
JonathanMerrittcommented, Mar 5, 2017

Sorry to tag on this issue, I am currently doing something else to achieve the same effect.

Is it a bad idea?

@ActivityScope @Subcomponent(modules = { MyActivityModule.class })
public interface MyActivityComponent extends AndroidInjector<MyActivity> {
  @Subcomponent.Builder abstract class Builder extends AndroidInjector.Builder<MyActivity> {
    public abstract Builder myActivityModule(MyActivityModule myActivityModule);

    @Override public void seedInstance(MyActivity instance) {
      myActivityModule(new MyActivityModule(instance));
    }

    public abstract MyActivityComponent build();
  }
}

If I should post on SO or create anew issue just tell me.

3reactions
ronshapirocommented, Mar 5, 2017

@AntonyGolovin I’d suggest you look at the linked issue that @JakeWharton mentioned.

@JonathanMerritt - that does work, but it’s unnecessary. the seedInstance method provides the activity instance into the graph, so you can have MyActivityModule with no state, and just request MyActivity in your @Provides methods.

class MyActivityModule {
  @Provides
  static SomethingDerivedFromMyActivity providesMethod(MyActivity myActivity) {
    return myActivity.somethingDerived();
  }
}

Doing this saves the module instance and allows the generated factories to be leaner.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Component - javadoc.io
Subcomponents are declared by listing the class in the Module.subcomponents() attribute of one of the parent component's modules. This binds the Subcomponent.
Read more >
dagger.BindsInstance Maven / Gradle / Ivy - Download JAR files
dagger.BindsInstance maven / gradle build tool code. The class is part of the package ➦ Group: com.google.dagger ➦ Artifact: dagger ➦ Version: 2.10-rc2....
Read more >
Dagger Android Migration Guide - Medium
To do that, we need another module that will just hold all the Activity modules. This module will then be included in the...
Read more >
Dagger 2 updates (specifically for Android) : r/androiddev - Reddit
Your "ActivityBindingModule" (the module that declares the Activity subcomponent builders) that is part of the ApplicationComponent can be swapped out in ...
Read more >
dagger-2.10-rc2-javadoc.jar下载及Maven、Gradle引入代码,pom ...
MF allclasses-noframe.html constant-values.html dagger/package-use.html dagger/Module.html dagger/package-frame.html dagger/Component.Builder.html ...
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