匕首2注入方法 [英] Dagger 2 inject method

查看:81
本文介绍了匕首2注入方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在玩匕首2

我有以下 Module

@Module
public class GameSetupModule {
    @Provides
    @Singleton
    GameSetup provideGameSetup() {
        return new GameSetup();
    }
}

和相应的 Component

@Singleton
@Component(modules = {GameSetupModule.class})
public interface GameSetupComponent {
    GameSetup provideGameSetup();

    void inject(SetupActivity activity);

//    void inject(Fragment fragment);

    void inject(SetupCompletedFragment fragment);

    void inject(SelectQuarterLengthFragment fragment);

    void inject(SelectTeamColorsFragment fragment);

    void inject(SelectUserRoleFragment fragment);

}

如您所见,GameSetup注入了几种不同的像这样的片段:

As you can see the GameSetup is to injected into several different Fragments like this:

@Inject
GameSetup gameSetup; 

onCreate(){
   getGameSetupComponent().inject(this); 
}

如上所示执行时效果很好,但是当我只使用一种方法

It works fine when implemented as seen above, the injection does not work though when I just use a single method

 void inject(Fragment fragment);

所有片段。

我是在做错什么,还是打算对 where GameSetup 可能被注入并且在可能不可用的地方?

Am I doing something wrong or is this even intended to have more control over where the GameSetup may be injected and where it may not be available?

推荐答案

Dagger2不支持基类注入

Dagger2 does not support base class injections out of the box.

诸如 void inject(Fragmentfragment); 之类的方法只会注入用 @Inject 属于 Fragment 类,而不是其子类。

A method such as void inject(Fragment fragment); would only inject the fields that are specified with @Inject within the Fragment class, and not its subclasses.

根据Reddit上的jackhexen,您正在做的 反射。但是反思会破坏Proguard。

According to jackhexen on Reddit, what you are doing is possible to do with reflection. But reflection can break Proguard.

我个人会投票支持此解决方案

这篇关于匕首2注入方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆