匕首2:在定义子组件后注入

编程入门 行业动态 更新时间:2024-10-18 10:14:01
本文介绍了匕首2:在定义子组件后注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我遵循此指南为我的Android应用程序制作子组件.在这里,我定义了一个用于LoginActivity的名为 LoginComponent 的子组件:

I follow this guide for making sub components for my Android application. Here I defined one sub component named LoginComponent used for LoginActivity:

@Subcomponent(modules = LoginModule.class) public interface LoginComponent { void inject(LoginActivity activity); @Subcomponent.Builder interface Builder { Builder requestModule(LoginModule module); LoginComponent build(); } } @Module public class LoginModule { @Provides LoginManager provideLoginManager(LoginManagerImpl manager) { return manager; } @Provides LoginView provideLoginView(LoginViewImpl view) { return view; } @Provides LoginPresenter loginPresenter(LoginView view, LoginManager manager) { return new LoginPresenterImpl(view, manager); } }

我在另一个组件中定义了这个子组件:

I define this sub component inside another component:

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

这是我的LoginActivity:

Here is my LoginActivity:

public class LoginActivity extends AppCompatActivity { @Inject LoginPresenter presenter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); } }

我的问题是:

  • @ Subcomponent.Builder 的目的.我不明白这一点,因为此组件中的所有模块都已在 @Subcomponent 批注中定义.为什么我们要再次复制.
  • 如何在上述活动中注入LoginPresenter.
  • purpose of @Subcomponent.Builder. I don't understand this point, because all modules in this component has been defined on @Subcomponent annotation. Why should we duplicate again.
  • how can I injected LoginPresenter on above activity.
  • 谢谢

    推荐答案

  • @ Subcomponent.Builder 遵循 @ Component.Builder 中记录的相同规则.:您需要为Dagger无法初始化的每个模块都有一个设置器.(允许模块具有构造函数参数或静态工厂方法,这将阻止Dagger创建其自己的实例.)

  • @Subcomponent.Builder follows the same rules documented in @Component.Builder: You need to have a setter for every Module that Dagger can't initialize itself. (Modules are allowed to have constructor parameters or static factory methods, which would prevent Dagger from creating its own instance.)

    由于Dagger可以通过调用零参数构造函数来创建LoginModule,因此可以从Builder中删除该方法;您不需要重新创建它.您可能还考虑使LoginModule的方法 static ,这将使您可以将LoginModule设置为接口或抽象类,然后Dagger完全可以避免持有对Module的引用.最后,对于绑定两个类的简单 @Provides 方法(例如, @Provides B ProvideB(A a){return a;} ),您可以切换到 @Binds ,这样可以使生成的代码更快.

    Because Dagger can create a LoginModule by calling a zero-arg constructor, you can drop that method from the Builder; you don't need to recreate it. You might also consider making LoginModule's methods static, which would allow you to make LoginModule an interface or abstract class—then Dagger would be able to avoid holding a reference to the Module at all. Finally, for the simple @Provides methods that bind two classes (e.g. @Provides B provideB(A a) { return a; }) you can switch to @Binds, which would allow the generated code to be even faster.

    此时,您的 @ Subcomponent.Builder 成为特定于子组件的 Provider :您可以注入 Builder 或提供程序< Builder> ,并通过在其上调用 build()来获取新的子组件实例.只要不需要提供任何Module实例,就不需要在构建器上使用任何其他方法.

    At that point, your @Subcomponent.Builder becomes a subcomponent-specific Provider: You can inject your Builder or a Provider<Builder> and get a new subcomponent instance by calling build() on it. As long as you don't need to provide any Module instances, you don't need to have any other methods on your builder.

    您也许可以简单地注入 Provider< LoginComponent> ,但是我在实践中没有看到它,也没有亲自尝试过.

    You may be able to simply inject a Provider<LoginComponent>, but I haven't seen that in practice, and haven't tried it myself.

    要注入LoginPresenter,您需要进入您的AppComponent,创建一个新的LoginComponent,然后使用它通过传递它来注入您的Activity.但是,这可能很困难,因为您没有给自己任何注入您的LoginComponent.Builder的访问权限.

    To inject a LoginPresenter, you need to get to your AppComponent, create a new LoginComponent, and use it to inject your Activity by passing it in. However, this may be difficult, because you haven't given yourself any access to inject your LoginComponent.Builder.

    除非您想尝试使用 dagger.android ,否则,您可以添加一个调用AppComponent来创建新的LoginComponent的方法.

    Unless you want to try with dagger.android instead, you can add a method to call on your AppComponent to create a new LoginComponent.

    // This way... LoginComponent createLoginComponent(); // or this way: LoginComponent.Builder createLoginComponentBuilder();

    有时,您会看到第一种名为 plus()的方式,这是Dagger 1中建立的命名约定.第一种方法还可以避免创建@ Subcomponent.Builder或在 AppModule 中添加子组件,尽管您可能希望按原样保留该结构,以便以后可以向AppModule中添加更多内容.

    Sometimes you'll see the first way named plus(), which is a naming convention established in Dagger 1; that first way also lets you avoid creating a @Subcomponent.Builder or adding the subcomponent in your AppModule, though you might want to leave the structure the way you have it so you can add more to your AppModule later.

    要结束此操作,请在 Activity.onCreate 中,让您的Activity获取应用程序,访问其AppComponent并进行自我注入.

    To finish it off, in Activity.onCreate, you can have your Activity get your Application, access its AppComponent, and inject itself.

    ((YourApplication) getContext().getApplicationContext()) .getApp() .createLoginComponent() .inject(this);

  • 更多推荐

    匕首2:在定义子组件后注入

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

    发布评论

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

    >www.elefans.com

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