Android Dagger get Parent Fragment into child fragment

Android Dagger get Parent Fragment into child fragment



I have been trying to, unsuccessfully, inject the Parent Fragment into its sub fragments for navigation purposes. I have followed a couple of different posts but I can't seem to understand what am I missing in my implementation.



I have a MainActivity that contains a ViewPager with one such page containing EventsFragment. This fragment in turn has two child fragments EventsListFragment and EventsDetailFragment. I wanted to inject EventsFragment fragment into EventsListFragment so that I can tell it to navigate to EventsDetailFragment.


MainActivity


ViewPager


EventsFragment


EventsListFragment


EventsDetailFragment


EventsFragment


EventsListFragment


EventsDetailFragment



I know this is probably a repetition of the posts below and I really apologize for that but I honestly cannot see what am I missing. These are the posts I've found:



My implementation is as follows:



ApplicationComponent


@Singleton
@Component(modules = [
AndroidSupportInjectionModule::class,
ActivityModule::class,
BaseApplicationModule::class,
ApplicationModule::class])
interface ApplicationComponent : AndroidInjector<AndroidApplication>

@Component.Builder
abstract class Builder : AndroidInjector.Builder<AndroidApplication>()



ActivityModule


@Module(includes = [MainActivityProvider::class])
abstract class ActivityModule




MainActivityProvider


@Module(includes = [EventsFragmentProvider::class])
abstract class MainActivityProvider
@PerActivity
@ContributesAndroidInjector(modules = [MainActivityModule::class])
abstract fun provideMainActivityFactory(): MainActivity



EventsFragmentProvider


@Module(includes = [EventsListProvider::class,
EventsDetailProvider::class])
abstract class EventsFragmentProvider
@PerFragment
@ContributesAndroidInjector(modules = [EventsFragmentModule::class])
abstract fun provideEventsFragmentFactory(): EventsFragment



EventsFragmentModule


@Module
class EventsFragmentModule
@Binds
abstract fun providesEventListView(eventsFragment: EventsFragment): EventContract.Flow



EventsListProvider


@Module
abstract class EventsListProvider
@PerFragment
@ContributesAndroidInjector(modules = [EventsListModule::class])
abstract fun provideEventsListFragmentFactory(): EventsListFragment



EventsFragment


class EventsFragment : DaggerFragment(), EventContract.Flow
override fun navigateToList()
addFragment(navigator.getEventsListFragment(context!!))


override fun navigateToDetail(id: String)
println("id = $id")


...



EventContract


interface EventContract
interface Flow
fun navigateToList()

fun navigateToDetail(id: String)




EventsListFragment


class EventsListFragment : DaggerFragment()
@Inject
lateinit var eventsFlow: EventContract.Flow

...



[Dagger/MissingBinding] [dagger.android.AndroidInjector.inject(T)] EventContract.Flow cannot be provided without an @Provides-annotated method.
public abstract interface ApplicationComponent extends dagger.android.AndroidInjector<AndroidApplication> {
^
EventContract.Flow is injected at
EventsListFragment.eventsFlow
EventsListFragment is injected at
dagger.android.AndroidInjector.inject(T)
component path: ApplicationComponent →EventsListProvider_ProvideEventsListFragmentFactory.EventsListFragmentSubcomponent



I may be using a anti-pattern but this is what got me working to this point. Im open to changes that may help me achieve this





what is the error?
– jaychang0917
Aug 27 at 1:54





Sorry just added the error
– riteshakya037
Aug 27 at 2:23





Your EventsListFragment needs an EventContract.Flow . For this reason, you must add the module capable of providing an EventContract.Flow (EventsFragmentModule) to the @ContributesAndroidInjector modules for that fragment. This said, I'm not sure how it's going to work
– natario
Aug 27 at 2:52





1 Answer
1



The error is caused by the following module:


@Module
class EventsFragmentModule
@Binds
abstract fun providesEventListView(eventsFragment: EventsFragment): EventContract.Flow



Dagger would not provide you with a Fragment and you shouldn't do that.


Fragment



Moreover, I think you misunderstand the meaning of @ContributesAndroidInjector. It means creating an AndroidInjector for you but proving an instance.


@ContributesAndroidInjector


AndroidInjector


@Module
abstract class EventsListProvider
@PerFragment
@ContributesAndroidInjector(modules = [EventsListModule::class])
abstract fun provideEventsListFragmentFactory(): EventsListFragment



So you should pass your EventsFragment instance into the module like this post instead of using field injection.


EventsFragment






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế