How can I provide an activity context with dagger dependencies?

How can I specify an activity context in the mainModule class? Thank! The code is as follows:

    @Singleton
@Component(modules = {AndroidInjectionModule.class, AppModule.class, ActivityBuilder.class})
public interface AppComponent {

    @Component.Builder
    interface Builder {

        @BindsInstance
        Builder application(Application application);

        AppComponent build();

    }

    void inject(MvmApp app);
}

Activation constructor:

@Module
public abstract class ActivityBuilder {

    @ContributesAndroidInjector(modules = {MainModule.class})
    abstract MainActivity bindMainActivity();
}

And I have an appModule and a module for each action:

@Module
public class AppModule {

    @Provides
    @Singleton
    @ApplicationContext
    Context provideContext(Application application) {
        return application;
    }

    @Provides
    @Singleton
    DataManager provideDataManager(AppDataManager appDataManager) {
        return appDataManager;
    }

    @Provides
    @DatabaseInfo
    String provideDatabaseName() {
        return "carDatabase";
    }

    @Provides
    @Singleton
    AppDataBase provideAppDatabase(@DatabaseInfo String dbName, @ApplicationContext Context context) {
        return Room.databaseBuilder(context, AppDataBase.class, dbName)
                .build();
    }

    @Provides
    @Singleton
    DbHelper provideDbHelper(AppDbHelper appDbHelper) {
        return appDbHelper;
    }
}

AppClass:

public class MvmApp extends Application implements HasActivityInjector {

    @Inject
    DispatchingAndroidInjector<Activity> activityDispatchingAndroidInjector;

    @Override
    public void onCreate() {
        super.onCreate();

        DaggerAppComponent.builder()
                .application(this)
                .build()
                .inject(this);

    }

    @Override
    public DispatchingAndroidInjector<Activity> activityInjector() {
        return activityDispatchingAndroidInjector;
    }
}

All my actions expand the basic activity, which introduces dependencies on the dagger.

public abstract class BaseActivity extends AppCompatActivity implements MvpView {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        AndroidInjection.inject(this);

    }
}

In mainModule, I need to provide an activity context.

@Module
public class MainModule {

    @Provides
    MainMvpPresenter<MainMvpView> provideMainPresenter(
            MainPresenter<MainMvpView> presenter) {
        return presenter;
    }



    @Provides
    CompositeDisposable provideCompositeDisposable() {
        return new CompositeDisposable();
    }

    @Provides
    CarAdapter provideCarAdapter( @ActivityContext Context context) {
        return new CarAdapter(context);
    }

}
+4
source share
3 answers

Decision:

@Module
public class MainModule {

    @Provides
    MainMvpPresenter<MainMvpView> provideMainPresenter(
            MainPresenter<MainMvpView> presenter) {
        return presenter;
    }

    @Provides
    CompositeDisposable provideCompositeDisposable() {
        return new CompositeDisposable();
    }

    @Provides
    CarAdapter provideCarAdapter(MainActivity activity) {
        return new CarAdapter(activity);
    }

}
0
source

For people facing a similar problem, I made a project with Kotlin and a new extension android-daggerwith a long explanation of how everything works here: https://github.com/Obaied/BareBonesAndroidDagger

+2

(dagger2 2.11)

@Provides
public Activity provideActivity(BaseActivity baseActivity) {
    return baseActivity;
}

CarAdapter

@Provides
CarAdapter provideCarAdapter(Activity context) {
    return new CarAdapter(context);
}

to your module, should do it. Please remember that

 AndroidInjection.inject(this);

should be called before super.onCreate(savedInstanceState);

0
source

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


All Articles