亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

無法使用 Dagger 2 提供活動上下文

無法使用 Dagger 2 提供活動上下文

慕姐8265434 2023-10-19 21:20:30
解決了我是 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 注入活動上下文的方法。這樣做的正確方法是什么?
查看完整描述

3 回答

?
森林海

TA貢獻2011條經驗 獲得超2個贊

您可以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)

? ? }

}


查看完整回答
反對 回復 2023-10-19
?
當年話下

TA貢獻1890條經驗 獲得超9個贊

@Module

class MainActivityModule(mActivity: MainActivity) {


    var mActivity: MainActivity



    init {

        this.mActivity= mActivity

    }



    @Singleton

    @Provides

    fun getMainActivity(): MainActivity{

        return mActivity

    }



}

現在將此模塊添加到您的組件類中,并在使用組件構建器時將您的活動傳遞給模塊構造函數希望這可以幫助您。


查看完整回答
反對 回復 2023-10-19
?
泛舟湖上清波郎朗

TA貢獻1818條經驗 獲得超3個贊

我們可以通過添加 @Named 注解來區分 ActivityModule 和 ContextModule 中的方法 context() ,如下所示:


@Module

public class ActivityModule {

    private final Context context;


    ActivityModule(Activity context){

        this.context = context;

    }


    @Named("activity_context")

    @Provides

    public Context context(){ return context; }

}


@Module

public class ContextModule {

    private final Context context;


    ActivityModule(Activity context){

        this.context = context;

    }


    @Named("application_context")

    @Provides

    public Context context(){ return context.getApplicationContext(); }

然后,我們告訴 Dagger 使用各自的 Context,如下所示:


@Module(includes = ContextModule.class)

public class OkHttpClientModule {

    ....

    @Provides

    public File file(@Named("application_context") Context context){

    File file = new File(context.getCacheDir(), "HttpCache");

    file.mkdirs();

    return file;

    }

    ....

}

希望這可以幫助。


查看完整回答
反對 回復 2023-10-19
  • 3 回答
  • 0 關注
  • 186 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號