Hi community I have problem with understanding dagger 2 adding subcomponent in the new way (added in dagger 2.7). See example below:
@Component(modules = {AppModule.class, MainActivityBinder.class})
@Singleton
interface AppComponent
{
inject(MyApplication _)
}
@Subcomponent(modules = ActivityModule.class)
interface ActivitySubcomponent
{
inject(MainActivity _)
@Subcomponent.Builder
interface Builder
{
@BindInstance
Builder activity(Activity activity)
ActivitySubcomponent build();
}
}
Initial step: I have AppComponent
with is my root component, that provide AppModule
with singletons (retrofit, okhttp etc.) In ActivitySubcomponent
I provide ActivityModule
with has dependencies specified to that activity. Now subcomponent must be added to AppComponent
, so in new way I create specified module called MainActivityBinder
, that has annotations @Module.subcomponents with point to that binds subcomponent, but I have first problem, what should be in body of that bind module ?
@Module(subcomponents = ActivitySubcomponent.class)
public class MainActivityBinder
{
//what body of this class should be ??
}
I know, that idea is that I can bind subcomponent or their builder. Second question when bind builder, and when bind subcomponent ? For example my ActivitySubcomponent
required activity context, so I create builder that provide context to ActivityModule
in this case will be better to provide in MainActivityBinder
a builder ? Third question how invoke component builder and how obtain subcomponent for app component ? In standard subcomponent factory I added to AppComponent
method that return subcomponent and I can define params (for example give activity context, listed below)
@Component(modules = {AppModule.class})
@Singleton
interface AppComponent
{
ActivitySubcomponents newActivitySubcomponents(Activity activity);
inject(MyApplication _);
}
// in MainActivity
appComponent.newActivitySubcomponents(this).build().inject(this);
so have achieve this behavior in new subcomponent added method ?