在Android上,使用Dagger2时,我必须在使用apiService
的每个活动上调用以下行:
@Inject
public ApiService apiService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DaggerApiComponent.builder()
.activityModule(new ActivityModule(this))
.build()
.inject(this);
//...
}
如何将其总结为:
DaggerApiComponent.builder()
.activity(this)
.build()
.inject(this);
或更简单:
MyApplication.injectApiService(this);
如何在活动中更改组件和模块以使用带有较少复制粘贴代码的Dagger2?
这是我的ApiComponent
:
@Singleton
@Component(modules = ApiModule.class)
public interface ApiComponent {
void inject(MainActivity activity);
void inject(...
}
这里是ApiModule
:
@Module(includes = {RetrofitModule.class, ActivityModule.class})
public class ApiModule {
@Singleton
@Provides
public static ApiService provideApiService(Activity activity) {
//...
}
}
和ActivityModule
:
@Module
public class ActivityModule {
private final Activity context;
public ActivityModule(Activity context) {
this.context = context;
}
@Singleton
@Provides
public Activity provideActivityContext() {
return context;
}
}
答案 0 :(得分:1)
您可以使用Android Activity注射器,用法here进行了详细说明。
答案 1 :(得分:1)
这种“ DI”的方法有两个问题:
Activity
)不应该知道@Inject
实例的来源,它关心的只是“嘿,给我一个实例”。为解决上述问题,dagger.android
来了。
AndroidInjector
。// App component
@Singleton
@Component(
modules = [
AndroidSupportInjectionModule::class, // build-in module
ActivityBindingModule::class,
AppModule::class
]
)
interface AppComponent : AndroidInjector<MainApplication> {
// we need to bind `MainApplication` instance to this component,
// so we have a builder here.
@Component.Builder
abstract class Builder : AndroidInjector.Builder<MainApplication>()
}
// Each controller (e.g. `Activity` / `Fragment` / `Service`) subcomponents
@Module
abstract class ActivityBindingModule {
// will generate a FooActivitySubcomponent under ActivityBindingModule's component
@ActivityScoped
@ContributesAndroidInjector(modules = [FooModule::class])
internal abstract fun fooActivity(): FooActivity
@ActivityScoped
@ContributesAndroidInjector(modules = [BarModule::class])
internal abstract fun barActivity(): BarActivity
}
AndroidInjector
,以便可以使用步骤1中提供的注射器为我们进行注射。// App component
class MainApplication : DaggerApplication() {
override fun applicationInjector(): AndroidInjector<out DaggerApplication> {
return DaggerAppComponent.builder().create(this)
}
}
// Each controller subcomponents
class FooActivity : DaggerAppCompatActivity() {
@Inject lateinit var foo: Foo
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// no need to call inject() here anymore!
foo.doSth()
}
}
举一个具体的例子:签出iosched