Java – dagger 2 activity injection does not work

I'm trying the new dagger 2. This is my first time to implement it, but I can't make it work I think I got the concept, and I understood the here example

I tried to copy the same structure, just a little modification for my example

This is the appcomponent that extends graph. I define the class I want

@ApplicationScope
@Component(modules = {AppModule.class,DataModule.class})
public interface EFAppComponent extends EFGraph {

    /**
     * An initializer that creates the graph from an application.
     */
    public final static class Initializer {

        private Initializer() {
        } // No instances.

        public static EFAppComponent init(EFApp app) {
            return Dagger_EFAppComponent.builder()
                    .appModule(new AppModule(app))
                    .build();
        }
    }
}


public interface EFGraph {

    public void inject(EFApp app);

    public ImageLoader imageLoader();

    public EventBus eventBus();

}

Then I provide corresponding classes in each module Everything is fine from here. Dagger seams build dagger correctly_ EFAppComponent.

Then initialize with the constructor in the application class

component = EFAppComponent.Initializer.init(this);
component.inject(this);

Then my goal is to inject imageloader and eventbus into my activity To do this, I created an activitycomponent

@ActivityScope
@Component(
        dependencies = EFAppComponent.class,modules = ActivityModule.class
)
public interface ActivityComponent {

    public void inject(BaseActivity activity);

}

Then I call you from my activities

activityComponent = Dagger_ActivityComponent.builder()
                .eFAppComponent(component)
                .activityModule(new ActivityModule(this))
                .build();
activityComponent.inject(this);

Because I should declare @ inject eventbus eventbus. In my activity after injecting the method Well, No

Therefore, after gradually debugging and tracking my applications and examples, I realized dagger_ Activitycomponent is not built correctly

private final ActivityModule activityModule;
    private final EFAppComponent eFAppComponent;

    private Dagger_ActivityComponent(Builder builder) {  
        assert builder != null;
        this.activityModule = builder.activityModule;
        this.eFAppComponent = builder.eFAppComponent;
        initialize();
  }

If the initialize method is empty and the provider is not declared as a variable

Did I miss anything? I've been trying to make it work all day, but I didn't succeed

Thank you for your help

Solution

Well... After spending the whole day, I decided to release it, and five minutes later I found a solution

I'm using baseactivity for all my activities, and I think I can use it to inject my components, but it's impossible

So I just changed this line in the activitycomponent

public void inject(BaseActivity activity)

to

public void inject(MainActivity activity)

So I want to inject the name of the dependency's activity

What makes me think of a new problem How do I implement the basic components to handle all my activities? Or do I have to create a component for each view / activity / fragment that I want to di?

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>