无法使用 Dagger 2 提供活动上下文

解决了


我是 Dagger 2 的新手,我正在尝试向类提供 Activity Context,但没有成功。我搜索了很多但没有找到任何合适的答案。


我可以提供应用程序上下文。但我还需要提供 Activity Context,但我不知道有什么好方法来实现它。


我需要澄清一下,我正在使用 Dagger 来处理 Android 依赖项。


def dagger_version = "2.24"

implementation "com.google.dagger:dagger:$dagger_version"

annotationProcessor "com.google.dagger:dagger-compiler:$dagger_version"

implementation "com.google.dagger:dagger-android:$dagger_version"

implementation "com.google.dagger:dagger-android-support:$dagger_version"

annotationProcessor "com.google.dagger:dagger-android-processor:$dagger_version"

我也只有一个带有以下代码的 AppComponent:


@Singleton

@Component(

        modules = {

                AndroidSupportInjectionModule.class,

                ActivityBuildersModule.class,

                AppModule.class,

                ViewModelFactoryModule.class,

        }

)

public interface AppComponent extends AndroidInjector<BaseApplication> {


    SessionManager sessionManager();


    @Component.Builder

    interface Builder{


        @BindsInstance

        Builder application(Application application);


        AppComponent build();

    }

}

除了我的每个活动都有一个模块,但我没有找到为 AppComponent 或从 ActivityModule 注入活动上下文的方法。


这样做的正确方法是什么?


慕姐8265434
浏览 91回答 3
3回答

森林海

您可以Activity按照当前绑定 实例的相同方式绑定 的实例Application,即使用 an@Component.Builder或@Component.Factory。示例实现如下所示:@Subcomponent(...)interface ActivitySubcomponent {    @Subcomponent.Factory    interface Factory {        ActivitySubcomponent create(@BindsInstance MyActivity activity)    }}@Module(subcomponents = [ActivitySubcomponent.class])class ApplicationModule {    ....}public class MyActivity extends Activity {    @Override    public void onCreate(Bundle savedInstanceState) {        ...        ((MyApplication) getApplication())            .applicationComponent            .activitySubcomponentFactory()            .create(this)            .inject(this)    }}

当年话下

@Moduleclass MainActivityModule(mActivity: MainActivity) {&nbsp; &nbsp; var mActivity: MainActivity&nbsp; &nbsp; init {&nbsp; &nbsp; &nbsp; &nbsp; this.mActivity= mActivity&nbsp; &nbsp; }&nbsp; &nbsp; @Singleton&nbsp; &nbsp; @Provides&nbsp; &nbsp; fun getMainActivity(): MainActivity{&nbsp; &nbsp; &nbsp; &nbsp; return mActivity&nbsp; &nbsp; }}现在将此模块添加到您的组件类中,并在使用组件构建器时将您的活动传递给模块构造函数希望这可以帮助您。

泛舟湖上清波郎朗

我们可以通过添加 @Named 注解来区分 ActivityModule 和 ContextModule 中的方法 context() ,如下所示:@Modulepublic class ActivityModule {&nbsp; &nbsp; private final Context context;&nbsp; &nbsp; ActivityModule(Activity context){&nbsp; &nbsp; &nbsp; &nbsp; this.context = context;&nbsp; &nbsp; }&nbsp; &nbsp; @Named("activity_context")&nbsp; &nbsp; @Provides&nbsp; &nbsp; public Context context(){ return context; }}@Modulepublic class ContextModule {&nbsp; &nbsp; private final Context context;&nbsp; &nbsp; ActivityModule(Activity context){&nbsp; &nbsp; &nbsp; &nbsp; this.context = context;&nbsp; &nbsp; }&nbsp; &nbsp; @Named("application_context")&nbsp; &nbsp; @Provides&nbsp; &nbsp; public Context context(){ return context.getApplicationContext(); }然后,我们告诉 Dagger 使用各自的 Context,如下所示:@Module(includes = ContextModule.class)public class OkHttpClientModule {&nbsp; &nbsp; ....&nbsp; &nbsp; @Provides&nbsp; &nbsp; public File file(@Named("application_context") Context context){&nbsp; &nbsp; File file = new File(context.getCacheDir(), "HttpCache");&nbsp; &nbsp; file.mkdirs();&nbsp; &nbsp; return file;&nbsp; &nbsp; }&nbsp; &nbsp; ....}希望这可以帮助。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java