Closed jega-ms closed 6 years ago
This appears to be a question - please use StackOverflow with the dagger-2
tag instead, as that is a better forum for questions.
@jega-ms Are you solve it? I face the same problem.
yes , you can refer this example https://github.com/jega-ms/android-dagger2-example
On Fri, Oct 19, 2018 at 9:24 AM SQSong notifications@github.com wrote:
@jega-ms https://github.com/jega-ms Are you solve it? I face the same problem.
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/google/dagger/issues/1249#issuecomment-431237029, or mute the thread https://github.com/notifications/unsubscribe-auth/APJShoENfM-OUjR6-AHnv6ARRt6pOBp7ks5umU0IgaJpZM4V4eUb .
-- Jegadeesan M
@songmao123 , To make it work , you have to comment out the "@Inject for Fragments constructor"
@jega-ms Thanks for your reply, bro.
Yes, my fragment's constructor has @Inject
annotation, but it didn't work. here are my code:
The application component:
@Singleton
@Component(modules = [AppModule::class, CommonModule::class, HttpModule::class,
AndroidSupportInjectionModule::class, ActivityBindingModule::class])
interface ApplicationComponent : AndroidInjector<BaseApplication> {
@dagger.Component.Builder
interface Builder {
@BindsInstance
fun application(application: Application): Builder
fun build(): ApplicationComponent
}
}
Then ActivityBindingModule
:
@Module
abstract class ActivityBindingModule {
@ActivityScope
@ContributesAndroidInjector(modules = [MainModule::class])
abstract fun contributeHomeActivity(): HomeActivity
}
The MainModule
for the HomeActivity
, HomeActivity
has four Fragment
:
@Module
abstract class MainModule {
@FragmentScope
@ContributesAndroidInjector(modules = [HomeModule::class])
abstract fun contributeHomeFragment(): HomeFragment
...
}
The HomeModule
for the first HomeFragment
:
@Module
class HomeModule {
@FragmentScope
@Provides
fun provideHomeView(homeFragment: HomeFragment): HomeContract.HomeView {
return homeFragment
}
@FragmentScope
@Provides
fun provideHomeModel(apiService: ApiService): HomeModel {
return HomeModel(apiService)
}
}
HomeFragment
implement HomeContract.HomeView
, and the constructor has @Inject
annotation:
class HomeFragment @Inject constructor() : BaseFragment<HomePresenter>(), HomeContract.HomeView,
SwipeRefreshLayout.OnRefreshListener, RecyclerScrollListener.OnLoadMoreListener {
...
}
BaseFragment
inject the Presenter
:
abstract class BaseFragment<P : IPresenter<*>> : DaggerFragment(), IView {
@Inject
lateinit var mPresenter: P
}
HomePresenter
code:
class HomePresenter @Inject constructor(private val homeModel: HomeModel,
private val homeView: HomeContract.HomeView,
private val disposable: CompositeDisposable) :
BasePresenter<HomeContract.HomeView, HomeContract.Model>(homeModel, disposable) {
...
}
But when i build the project, it still tips:
HomeContract.HomeView cannot be provided without an @Provides-annotated method.
Like you said, the Activity
inject View
is ok. but in the fragment, it was compile error, i don't know why?
i didn't get chance to get deeper into this issue. In my case, i just removed @Inject constructor annotation on all the fragments, later it's started working fine.
On Sat, Oct 20, 2018 at 6:19 PM SQSong notifications@github.com wrote:
@jega-ms https://github.com/jega-ms Thanks for your reply, bro.
Yes, my fragment's constructor has @Inject annotation, but it didn't work. here are my code: The application component:
@Singleton@Component(modules = [AppModule::class, CommonModule::class, HttpModule::class, AndroidSupportInjectionModule::class, ActivityBindingModule::class])interface ApplicationComponent : AndroidInjector
{ @dagger.Component.Builder interface Builder { @BindsInstance fun application(application: Application): Builder fun build(): ApplicationComponent } } Then ActivityBindingModule:
@Moduleabstract class ActivityBindingModule { @ActivityScope @ContributesAndroidInjector(modules = [MainModule::class]) abstract fun contributeHomeActivity(): HomeActivity }
The MainModule for the HomeActivity, HomeActivity has four Fragment:
@Moduleabstract class MainModule { @FragmentScope @ContributesAndroidInjector(modules = [HomeModule::class]) abstract fun contributeHomeFragment(): HomeFragment ... }
The HomeModule for the first HomeFragment:
@Moduleclass HomeModule { @FragmentScope @Provides fun provideHomeView(homeFragment: HomeFragment): HomeContract.HomeView { return homeFragment } @FragmentScope @Provides fun provideHomeModel(apiService: ApiService): HomeModel { return HomeModel(apiService) } }
HomeFragment implement HomeContract.HomeView, and the constructor has @Inject annotation:
class HomeFragment @Inject constructor() : BaseFragment
(), HomeContract.HomeView, SwipeRefreshLayout.OnRefreshListener, RecyclerScrollListener.OnLoadMoreListener { ... } BaseFragment inject the Presenter:
abstract class BaseFragment<P : IPresenter<*>> : DaggerFragment(), IView { @Inject lateinit var mPresenter: P }
HomePresenter code:
class HomePresenter @Inject constructor(private val homeModel: HomeModel, private val homeView: HomeContract.HomeView, private val disposable: CompositeDisposable) : BasePresenter<HomeContract.HomeView, HomeContract.Model>(homeModel, disposable) { ... }
But when i build the project, it still tips: HomeContract.HomeView cannot be provided without an @Provides-annotated method.
Like you said, the Activity inject View is ok. but in the fragment, it was compile error, i don't know why?
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/google/dagger/issues/1249#issuecomment-431577539, or mute the thread https://github.com/notifications/unsubscribe-auth/APJShougu1AFwanwYGNKJJX-TsCsoPOsks5umxvxgaJpZM4V4eUb .
-- Jegadeesan M
"@ContributesAndroidInjector" could not inject the fragment view interface on presenter constructor (MVP) but the same approach works fine with activity flow.
Error.
Source Code https://github.com/jega-ms/android-dagger2-mvp-rx/blob/master/app/src/main/java/com/jega/android/example/dagger2/MainActivity.java