Skip to content

Dagger ‐ Field injection

Devrath edited this page Oct 5, 2023 · 9 revisions

Observations

  • You can clearly see there are three implementations(Wheels, Engine, and Car).
// Dagger generates the component from it we can build the component 
val mobileComponent = DaggerMobileComponent.builder().build()
// Here we pass the context of the activity since we need the context for field injection for dagger
mobileComponent.inject(this@DaggerConceptsActivity)
// Access the methods of the class from the field injected variable
mobile.runMobile()

Output

Battery constructor is invoked !
Screen constructor is invoked !
Mobile constructor is invoked !
Mobile is running !

Clone this wiki locally