Dagger Field Injection

This is my note for Udemy lesson: Advanced Android Bootcamp 2024.
In this article we introduced created an abstract function getSmartPhone() and call it to get SmartPhone instance. We can also inject it into another class. This article I'll introduce you this way.

1. Modify SmartPhoneComponent

@Component(modules = [MemoryCardModule::class, NCBatteryModule::class])
interface SmartPhoneComponent {
    fun inject(mainActivity: MainActivity)
}

2. Inject in MainActivity

class MainActivity : AppCompatActivity() {
    @Inject
    lateinit var smartPhone: SmartPhone

    override fun onCreate(savedInstanceState: Bundle?) {
        ...
        DaggerSmartPhoneComponent.create().inject(this)
        smartPhone.makeACallWithRecording()
    }
}