使用dagger 2,我在Activity生命周期范围图中创建了一个Application生命周期范围图 . 在活动图中,我想为所有活动提供相同的Picasso实例
的AppModule:
@Module
public final class AppModule {
Application application;
public AppModule(Application application) {
this.application = application;
}
@Provides
@Singleton
Context provideContext() {
return application;
}
}
AppComponent:
@Component(modules = {AppModule.class})
@Singleton
public interface AppComponent {
ActivityComponent activityComponent(ActivityModule activityModule);
}
ActivityModule:
@Module
public final class ActivityModule {
@Provides
@Singleton
Picasso providePicasso(Context context, OkHttpClient client) {
return new Picasso.Builder(context).downloader(new
OkHttp3Downloader(client)).build();
}
ActivityComponent:
@Subcomponent(modules = ActivityModule.class)
@PerActivity
public interface ActivityComponent {
FragmentComponent fragmentComponent(FragmentModule fragmentModule);
}
ActivityModule中出现错误,提供了抱怨如果没有@ Provide-annotated方法就无法提供Context的错误 .
ActivityComponent是AppComponent的子组件,它应该可以访问其父级中的所有内容,而AppModule通过provideContext提供Context而不是吗?