Let's say I have multiple activities and every activity contains separate Dagger2 component. So FooActivity has FooComponent and BarActivity has BarComponent. Also I have some viewgroup let's say BazViewGroup that can be used in both activities and must receive some dependencies through dagger graph. What is best way to inject dependencies into instance of this viewgroup since we don't know in advance in what activity it belongs?
Right now I have the following setup:
- Every activity exposes method
getComponent()which returns component contained in this activity Every component extends interface like
public interface CanInjectIntoBazViewGroup { void inject(BazViewGroup viewgroup); }Inside BazViewGroup constuctor I get reference to activity get component from it, cast it to
CanInjectIntoBazViewGroupand callinjectmethod.
Is there is a better way to do this?