Android Using a single context for multiple Dagger2 modules

I am new to use Dagger2for Android. I am creating some class as the Dagger module that they use context, I cannot merge, merge or use the same context for the other modules they need. and I get this error now:

android.content.Context is bound multiple times

SpModules:

@Module
public class SpModules {
    private Context context;

    public SpModules(Context context) {
        this.context = context;
    }

    @Provides // this can be non-scoped because anyway the same instance is always returned
    Context provideContext() {
        return this.context;
    }

    @Provides
    @Singleton
    SP provideSharePreferences(Context context) {
        return new SP(context); // use method-local Context
    }
}

RealmModule:

@Module
public class RealmModule {
    private Context context;

    @Provides
    Context provideApplicationContext() {
        return AlachiqApplication.getInstance();
    }

    @Provides
    @Singleton
    RealmConfiguration provideRealmConfiguration() {
        final RealmConfiguration.Builder builder = new RealmConfiguration.Builder()
                .schemaVersion(Migration.SCHEMA_VERSION)
                .deleteRealmIfMigrationNeeded()
                .migration(new Migration());
        return builder.build();
    }

    @Provides
    Realm provideDefaultRealm(RealmConfiguration config) {
        return Realm.getInstance(config);
    }

    @Provides
    Context provideContext() {
        return this.context;
    }
}

component:

@Component(modules = {RealmModule.class, SpModules.class})
@Singleton
public interface ApplicationComponent {
    void inject(ActivityRegister target);

    void inject(ActivityMain target);

    void inject(ActivityBase target);

    void inject(FragmentAlachiqChannels target);

    void inject(SocketServiceProvider target);
}

and then the Application class to create Dagger2:

   component = DaggerApplicationComponent.builder()
           .appModules(new SpModules(this))
           .build();

how can i solve this problem?

+4
source share
1 answer

, , . , :

@Provides
Context provideApplicationContext() {
    return AlachiqApplication.getInstance();
}

@Provides 
Context provideContext() {
    return this.context;
}

, , Context .

. , .

, , - . Named. , , , . :

@Module
public class SpModules {
   // ...

   @Provides
   @Named("context")
   Context provideContext() {
      return this.context;
   }

   //...
}

@Module
public class RealmModule {
   //...

   @Provides
   @Named("application.context")
   Context provideApplicationContext() {
      return AlachiqApplication.getInstance();
   }

   //...
}

, , , , :

public class Something {
   public Something(@Named("application.context") Context context) {
     //...
   }
}

:

public class Something {
   @Named("application.context") Context context;
   // ...
}

:

@Provides
@Singleton
SP provideSharePreferences(@Named("context") Context context) {
    return new SP(context);
}

, .

Qualifiers. Named, . . , , 2 :

@java.lang.annotation.Documented
@java.lang.annotation.Retention(RUNTIME)
@javax.inject.Qualifier
public @interface InstanceContext {
}

@java.lang.annotation.Documented
@java.lang.annotation.Retention(RUNTIME)
@javax.inject.Qualifier
public @interface ApplicationContext {
}

:

@Module
public class SpModules {
   // ...

   @Provides
   @InstanceContext
   Context provideContext() {
      return this.context;
   }

   //...
}

@Module
public class RealmModule {
   //...

   @Provides
   @ApplicationContext
   Context provideApplicationContext() {
      return AlachiqApplication.getInstance();
   }

   //...
}

, . :

public class Something {
   public Something(@ApplicationContext Context context) {
     //...
   }
}

public class Something {
   @ApplicationContext Context context;
   // ...
}

@Provides
@Singleton
SP provideSharePreferences(@InstanceContext Context context) {
    return new SP(context);
}

, . , . , .

+3

Source: https://habr.com/ru/post/1658800/


All Articles