Şu anda bir etkinlikte örneğin Picasso örneğini elde etmek için, AppComponent'e enjekte etme yöntemini eklemem gerekiyor.Her görünüm için enjeksiyon yönteminin eklenmesi nasıl engellenir?
AppComponent.class: Ben enjekte edilmelidir parçaları ve çok sayıda görüntüleme var çünkü, enjekte yönteminin eklemekten kaçının Nasıl
@ForApplication
@Singleton
@Component(
modules = {AppModule.class,OkHttpClientModule.class,NetworkApiModule.class,NetworkAuthModule.class})
public interface AppComponent {
void inject(Fragment1 obj);
void inject(Fragment2 obj);
void inject(Fragment3 obj);
void inject(Fragment4 obj);
void inject(Fragment5 obj);
void inject(Fragment6 obj);
...
}
Fragment1.class
public class Fragment1 extends Fragment {
@Inject Picasso mPicasso;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyApplication.getComponent(getContext()).inject(this);
}
}
Benim sınıfları:
AppModule.class:
@Module
public class AppModule {
private MyApplication mApplication;
public AppModule(@NonNull MyApplication mApplication) {
this.mApplication = mApplication;
}
@Provides
@NonNull
@Singleton
public Application provideApplication() {
return mApplication;
}
@Provides
@ForApplication
Context provideContext(){
return mApplication;
}
@Provides
@Singleton
Picasso providesPicasso(@ForApplication Context context) {
return new Picasso.Builder(context).build();
}
}
ForApplication.class:
@Scope
@Retention(RUNTIME)
public @interface ForApplication {
}
MyApplication.class
public class MyApplicationextends Application {
static Context mContext;
private AppComponent component;
@Override
public void onCreate() {
super.onCreate();
mContext = this;
setupComponent();
}
private void setupComponent() {
component = DaggerAppComponent.builder().appModule(new AppModule(this)).build();
component.inject(this);
}
public AppComponent getComponent() {
if (component==null)
setupComponent();
return component;
}
public static AppComponent getComponent(Context context) {
return ((MyApplication) context.getApplicationContext()).getComponent();
}
GÜNCELLEME
Ben de parçaları için adaptörleri enjekte ve ben ekleyeceğiz o zaman BaseFragment için enjekte BaseFragment istiyorum tüm çocuk parçaları
Ben parçaları için adaptörler enjekte etmek istediğiniz bir Aktivite bunu çözdü ve BaseFragment için enjekte ekleriz o zaman BaseFragment olacak Tüm çocuk parçaları için tüm adaptörler – NickUnuchek
Bu Adaptörler, picasso sağlayıcınızla aynı bileşende erişilebilir mi? Bu adaptörler birbirinden nasıl farklıdır? – Jacob
Sanırım @Jacob masaya iyi bir cevap getiriyor, ama eğer başka üyeler enjekte etmeniz gerekiyorsa, hepsini ayrı ayrı yapmak için seçeneğiniz yok! –