Dependencies from two components in one activity

前端 未结 1 1187
情话喂你
情话喂你 2021-02-09 20:44

I\'m playing with Dagger-2 to figure how to integrate it in our existing application and I\'m facing something which I can\'t understand or I\'m doing wrong.

My situatio

相关标签:
1条回答
  • 2021-02-09 21:09

    Ah...I share your pain. You can't inject two components into same Activity. At least I was not able to find how! :)

    That was logical for me to try, as well, and I faced same problems.

    You can resolve it with 2 approach:

    1. Use one big component for all injections (I am really agains that)
    2. Create one component for each Activity/place you want to inject something

    So, instead of

    @Singleton
    @Component(modules = {RestModule.class, SocketModule.class})
    public interface NetComponent {
       void inject(MainActivity activity);
    }
    

    try with

    @Singleton
    @Component(modules = {RestModule.class, SocketModule.class, DbModule.class})
    public interface SecondActivityComponent {
       void inject(SecondActivity activity);
    }
    

    I found second one as cleanest solution.

    If anybody has some other advice, I am ready to try it!

    0 讨论(0)
提交回复
热议问题