简体   繁体   English

匕首2注射儿童班

[英]Dagger 2 Inject Child Class

I understand that with Dagger 2 I cannot inject in a base class and expect it to carry over to the child class. 据我所知,对于Dagger 2,我无法注入基类并希望它能够延续到子类。 But why can I not call inject inside of a child class unless I explicitly have an inject method for that class? 但是为什么我不能在子类中调用注入,除非我明确地为该类inject了一个方法?

I tried following the example in this article: Dagger 2: Even Sharper, Less Square . 我尝试按照本文中的示例进行操作: Dagger 2:Even Sharper,Less Square This solution should allow me to call inject in a subclass, but when I test it I get a NullPointerException for all of my @Inject targets. 这个解决方案应该允许我在子类中调用inject,但是当我测试它时,我得到了所有@Inject目标的NullPointerException

public abstract class BaseFragment extends Fragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        injectComponent(MyApplication.getComponent());
    }

    protected abstract void injectComponent(AppComponent component);
}

public class MyFragment extends BaseFragment {

    @Inject MyDependency mDependency;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
        mDependency.doSomething(); // NullPointerException
        ...
    }

    ...

    @Override
    public void injectComponent(AppComponent component) {
        component.inject(this);
    }
}

Is there another way to inject into each subclass without creating an inject method for each an every class? 是否有另一种注入每个子类的方法,而不为每个类创建一个注入方法? Or is that the only way Dagger 2 will work? 或者这是Dagger 2的唯一工作方式吗? If that is the case, I will end up with an absurdly long Component class. 如果是这种情况,我将得到一个荒谬的长Component类。 With an inject method for every Activity , Fragment or various other helper classes: 使用每个ActivityFragment或其他各种帮助程序类的inject方法:

@Singleton @Component(modules = {AppModule.class})
public interface AppComponent {

    void inject(MyClass clazz);

    void inject(MyClass2 clazz);

    ...

    void inject(MyClassN clazz);
}

I would much rather have to call component.inject() in every class, than have to do that along with another inject method. 我宁愿不得不在每个类中调用component.inject() ,而不是像另一个inject方法一样。


Although similar, I do not believe my question is a duplicate Dagger 2 Activity Injection Not Working . 虽然相似,但我不相信我的问题是重复的Dagger 2活动注射不起作用 That question asks why the injection was not working, to which the answer would be: because Dagger 2 has a strong type-association and you must declare an inject method for each and every class. 这个问题询问为什么注入不起作用,答案是:因为Dagger 2有一个强类型关联,你必须为每个类声明一个inject方法。 This question focuses more on a way around the boilerplate, while hopefully maintaining strong type-association. 这个问题更多地侧重于围绕样板的方法,同时希望保持强大的类型关联。

I found a nifty little trick, used in the AsymmetricFingerprintDialog sample application, though I do not know if I fully understand how it works. 我发现了一个漂亮的小技巧,在AsymmetricFingerprintDialog示例应用程序中使用,但我不知道我是否完全理解它是如何工作的。 The sample uses the first iteration of Dagger, but a quick test showed that it works with Dagger 2 as well. 该示例使用Dagger的第一次迭代,但快速测试表明它也适用于Dagger 2。

For each Fragment I added an empty constructor with an @Inject annotation, then used an @Inject annotation to get an instance of the Fragment in my Activity . 对于每个Fragment我添加了一个带有@Inject批注的空构造函数,然后使用@Inject批注在我的Activity获取Fragment的实例。

public class MainActivity extends AppCompatActivity {

    @Inject MyFragment mFragment;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.getComponent().inject(this);
        getSupportFragmentManager().beginTransaction
                .replace(R.id.fragment_container, mFragment).commit();
        ...
    }
}

public class MyFragment extends Fragment {

    @Inject MyDependency mDependency;

    @Inject
    public MyFragment() {}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
        mDependency.doSomething();
        ...
    }
}

This allows me to call MyApplication.getComponent().inject(this) only in each Activity , and the dependencies will be injected to the Fragment classes as well. 这允许我只在每个Activity调用MyApplication.getComponent().inject(this) ,并且依赖关系也会被注入到Fragment类中。 In this way, I only need an inject method for each Activity , and not each Fragment . 这样,我只需要为每个Activity inject一个方法,而不是每个Fragment

@Singleton @Component(modules = {AppModule.class})
public interface AppComponent {

    void inject(MainActivity activity);
}

I am curious as to how this works though. 我很好奇这是如何工作的。 From what I can see it has nothing to do with Dagger, instead it seems to use the @Inject annotation for its intended purpose? 从我所看到的它与Dagger无关,相反它似乎使用@Inject注释用于其预期目的? Though I am new to Dagger as well as JSR-330, so I am unsure. 虽然我是Dagger和JSR-330的新手,但我不确定。


Edit: This method only works with setRetainInstanceState(true) , or if you save and restore the instance yourself. 编辑:此方法仅适用于setRetainInstanceState(true) ,或者您自己保存和还原实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM