简体   繁体   English

Dagger2组件注入多个活动

[英]Dagger2 Component inject for multiple Activities

This seems very basic question for Dagger2 users . 对于Dagger2用户来说,这似乎是一个非常基本的问题。 I have recently started exploring it with RetroFit . 我最近开始使用RetroFit进行探索。 I have followed some tutorials and came up with the code below(some of it). 我遵循了一些教程,并提出了以下代码(其中一些)。

    @Singleton
    @Component(modules = {AppModule.class, ApiModule.class})
     public interface ApiComponent {
    void inject(MainActivity context);
     }


    public class MyApplication extends Application {
    private ApiComponent mApiComponent;
    @Override
    public void onCreate() {
        super.onCreate();
        mApiComponent = DaggerApiComponent.builder()
                .appModule(new AppModule(this))
                .apiModule(new ApiModule("https://rect.otp/demos/"))
                .build();
    }
    public ApiComponent getNetComponent() {
        return mApiComponent;
    }
   }

And MainActivity.java 和MainActivity.java

public class MainActivity extends AppCompatActivity {
@Inject
Retrofit retrofit;
ActivityMainBinding mainBinding;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
    ((MyApplication) getApplication()).getNetComponent().inject(this);
    ApiCall api = retrofit.create(ApiCall.class);
}
}

Questions 问题
1. When i change void inject(MainActivity context); 1.当我更改void inject(MainActivity context); to void inject(Context context); void inject(Context context); i am getting a NullPointerException on retrofit in MainActivity .Why? 我在MainActivity进行retrofit时得到了NullPointerException 。为什么?

  1. When use void inject(MainActivity context); 当使用void inject(MainActivity context); its working fine. 它的工作正常。 Why ? 为什么呢

  2. If i need to inject RetroFit in Multiple classes what should be the approach. 如果我需要在多个类中注入RetroFit ,应该采用哪种方法。 Creating inject() for each class is not seems the solution. 为每个类创建inject()似乎不是解决方案。

I am a newbie to dependency Injections. 我是依赖注入的新手。 So Can i have some guidence on it . 因此,我可以对此进行一些指导吗? What will be the proper approach to use it in multiple classes. 在多个类中使用它的正确方法是什么?

When you declare void inject(Context context) Dagger will generate code to inject Context . 当您声明void inject(Context context) Dagger将生成代码以注入Context Since Context does not declare any @Inject annotated fields it will end up injecting nothing. 由于Context没有声明任何@Inject注释的字段,因此最终将不会注入任何内容。 This is why your retrofit is null after the injection. 这就是注射后您的retrofitnull原因。

When you declare void inject(MainActivity context) it will generate code to inject MainActivity that will also set your retrofit , thus it will be initialized. 当您声明void inject(MainActivity context) ,它将生成用于注入MainActivity代码,该代码还将设置您的retrofit ,因此将对其进行初始化。

Dagger will inject parent fields, but not childrens. 匕首将注入父域,但不会注入子域。 The class that you declare is the one that the code will be generated for. 您声明的类是将为其生成代码的类。

Your default way to inject objects should be Constructor Injection where you don't have to manually declare methods or inject the objects. 注入对象的默认方式应该是构造器注入 ,而不必手动声明方法或注入对象。 eg see this answer for reference . 例如,请参阅此答案以供参考

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

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