@Named annotation in Kotlin

Other topics

Declaring a qualified dependency

@Module
class AppModule(val app: Application) {

    @Provides @Named("the_answer")
    fun providesTheAnswer(): Int { 
        return 42
    }
}

Setter based dependency injection

class MyClass{
    @field:[Inject Named("the_answer")] lateinit var answer: Int
}

In Android Development, this is the way in which you inject dependencies into Activity, Fragment or any other object that is instantiated directly by the OS.

To learn more about the @field: annotation in Kotlin visit the documentation

Constructor based dependency injection

class MyClass @Inject constructor(@Named val answer: Int){
    /* The nuts and bolts of your class */
}

Contributors

Topic Id: 10812

Example Ids: 32422,32423,32424

This site is not affiliated with any of the contributors.