匕首2注入方法

编程入门 行业动态 更新时间:2024-10-19 08:49:34
本文介绍了匕首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注入方法

本文发布于:2023-10-31 04:28:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545004.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:匕首   方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!