我有一个 Android 应用程序,我正在尝试使用匕首。我决定使用全局图 + 活动图。我有三个模块:
安卓模块
@Module(library = true)
public class AndroidModule {
private final MApplication mApplication;
public AndroidModule(MyApplication application) {
mApplication = application;
}
@Provides @Singleton @ForApplication Context provideApplicationContext() {
return mApplication;
}
}
我的应用模块
@Module(
injects = {
MainActivity.class,
AddFragment.class,
AddMapFragment.class,
MyActivity.class,
ListFragment.class,
MyMapFragment.class,
RetainFragment.class,
SingleActivity.class,
UserActivity.class,
UserLoginFragment.class,
UserProfileActivity.class,
UserProfileFragment.class,
UserRegisterFragment.class,
WelcomeFragment.class
},
complete = false,
library = true,
includes = ActivityModule.class
)
public class MyAppModule {
private final Bus mBus = new Bus();
@Provides @Singleton Bus provideBus() {
return mBus;
}
@Provides @Singleton
RequestQueue provideRequestQueue(@ForApplication Context context) {
return Volley.newRequestQueue(context);
}
@Provides @Singleton Api provideApi(RequestQueue queue) {
return new AppApi(queue);
}
@Provides Logger provideLogger() {
return new MyLogger();
}
}
活动模块
@Module(
complete = false,
library = true
)
public class ActivityModule {
private BaseActivity mActivity;
public ActivityModule() {}
public ActivityModule(BaseActivity activity) {
mActivity = activity;
}
@Provides @Singleton @ForActivity Context provideActivityContext() {
return mActivity;
}
@Provides @Singleton LocationClientFactory provideLocationClientFactory() {
return new LocationClientFactory(mActivity);
}
}
我不得不MyAppModule
对. ActivityModule
_ Context
_Activity
Context
图表配置是否正确?我将模块添加到图中Application
和 a 中BaseActivity
,我认为这就足够了,我不需要包含,但我不确定我是否完全理解它的配置过程。
编辑
像这样,匕首没有错误,但如果我尝试注入一个Activity
Context
,在运行时它是null
.