T
T
TikTak12362018-11-19 10:26:14
Android
TikTak1236, 2018-11-19 10:26:14

How to pass AppCompatActivity to module?

Hello!
I use Dagger 2 in my project. I have several modules in my project.
And I have a BaseActivityModule which is connected to each Activity module.
How to get AppCompatActivity in BaseActivityModule.
When compiling the project I get the following error

error: [Dagger/MissingBinding] [dagger.android.AndroidInjector.inject(T)] android.app.AlertDialog cannot be provided without an @Inject constructor or an @Provides-annotated method.
public abstract interface AppComponent {
                ^
      android.app.AlertDialog is injected at
          com.example.laptop.daggerexample.ui.main.view.MainActivity.alertDialog
      com.example.laptop.daggerexample.ui.main.view.MainActivity is injected at
          dagger.android.AndroidInjector.inject(T)
  component path: com.example.laptop.daggerexample.di.component.AppComponent ? com.example.laptop.daggerexample.di.builder.ActivityBuilder_BindMainActivity.MainActivitySubcomponent

Example below modules in project:
ActivityBuilder
@Module
abstract class ActivityBuilder {
    @ContributesAndroidInjector(modules = [(MainActivityModule::class)])
    abstract fun bindMainActivity(): MainActivity
}

AppModule
@Module
abstract class AppModule{
    @Provides
    @Singleton
    fun provideContext(application: Application): Context = application
}

BaseActivityModule
@Module
class BaseActivityModule {
    @Provides
    fun provideAlertDialog(activity: AppCompatActivity)= AlertDialog.Builder(activity).create()
}

MainActivityModule
@Module(includes = [(BaseActivityModule::class)])
class MainActivityModule {
    @Provides
    fun provideMainMVPView(activity: MainActivity): MainMVPView = activity

    @Provides
    fun provideMainMVPPresenter(presenter: MainPresenter<MainMVPView>): MainMVPPresenter<MainMVPView> = presenter
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question