Ich bin mit der Fertigstellung Dagger 2 Retrofit2 Implementierung mit mvp stecken, wo meine App Aufrufe an zwei verschiedene Apis, in jeweiligen Fragmente. I haben Qualifier Annotationen definiert eine Instanz von Retrofit für jede API-Aufruf zu binden, wie untenDolch 2 mehrere Retrofit-Instanzen mit verschiedenen URLs für das Schlagen verschiedener APIs mit Konstruktor Injektion
@Qualifier
@Retention(RUNTIME)
public @interface FirstApi{}
@Qualifier
@Retention(RUNTIME)
public @interface SecondApi{}
jede Retrofit-Bindung in der ApplicationModule
public class ApplicationModule {
private String mBaseUrlFirstApi;
private String mBaseUrlSecondApi;
private Context mContext;
public ApplicationModule(Context context) {
mContext = context;
}
...
@Provides
@FirstApi Retrofit provideFirstApiRetrofit(…) {…}
@Provides
@SecondApi Retrofit provideSecondApiRetrofit(…) {…}
....
}
ApplicationComponent
@Singleton
@Component(modules = ApplicationModule.class)
public interface ApplicationComponent {
Retrofit exposeRetrofit();
Context exposeContext();
}
Scope
@Scope
@Retention(RetentionPolicy.RUNTIME)
public @interface PerActivity {
}
Providing ApiService
@Module
public class MyModule {
@PerActivity
@Provides
MyAPIService myApiService(Retrofit retrofit) {
return retrofit.create(MyAPIService.class);
}
}
Dolch Injector Klasse
@PerActivity
@Component(modules = MyModule.class, dependencies = ApplicationComponent.class)
public interface MyComponent {
void inject(Fragment1 fragment1);
void inject(Fragment2 fragment2);
// void inject(Fragment3 fragment3);
}
In der Anwendungsklasse, für einen einzelnen Retrofit-API-Aufruf, weiß, dass ich tun:
public class MyApplication extends Application {
private ApplicationComponent mApplicationComponent;
{...}
private void initializeApplicationComponents() {
mApplicationComponent = DaggerApplicationComponent
.builder()
.applicationModule(new ApplicationModule(this, "http://api1"))
.build();
}
{...}
public ApplicationComponent getApplicationComponent() {
return mApplicationComponent;
}
}
Aber für mehrere URL-API-Aufrufe funktioniert dies nicht
public class MyApplication extends Application {
private ApplicationComponent mApplicationComponent;
{...}
private void initializeApplicationComponents() {
mApplicationComponent = DaggerApplicationComponent
.builder()
.applicationModule(new ApplicationModule(this, "http://api1"))
.applicationModule(new ApplicationModule(this, "http://api2"))
.build();
}
{...}
public ApplicationComponent getApplicationComponent() {
return mApplicationComponent;
}
}
Weder tut dies
public class MyApplication extends Application {
private ApplicationComponent mApplicationComponent;
{...}
private void initializeApplicationComponents() {
mApplicationComponent = DaggerApplicationComponentApi1
.builder()
.applicationModule(new ApplicationModule(this, "http://api1"))
.build();
mApplicationComponent = DaggerApplicationComponentApi2
.builder()
.applicationModule(new ApplicationModule(this, "http://api2"))
.build();
}
{...}
public ApplicationComponent getApplicationComponent() {
return mApplicationComponent;
}
}
Auflösen in jedem Fragment Dolch Abhängigkeit wie
so getan wirdprotected resolveDaggerDependency() {
DaggerFragmentComponent
.builder()
.applicationComponent(getApplicationComponent)
.myModule(new MyModule.class)
.build.inject(this);
}
BaseFragment Klasse
public abstract class BaseFragment extends Fragment {
{...}
{...}
{...}
@CallSuper
protected void onViewReady(Bundle savedInstanceState, Intent intent) {
resolveDaggerDependency(); // to be used by child fragments
protected ApplicationComponent getApplicationComponent() {
return ((MyApplication)getActivity().getApplication()).getApplicationComponent;
}
}