Dependencies from two components in one activity

痴心易碎 提交于 2019-12-03 07:47:00

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!

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!