Skip to content

Dagger ‐ multi binding IntoSet

Devrath edited this page Oct 8, 2023 · 7 revisions

Observations

Output

Code

Scopes

Implementations

Modules

Components

Application

Activity

MyActivity.kt

class DaggerMultiBindingIntoSetActivity : AppCompatActivity() {

    private lateinit var binding: ActivityDaggerMultiBindingIntoSetBinding

    @Inject lateinit var telivision: Set<@JvmSuppressWildcards Telivision>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityDaggerMultiBindingIntoSetBinding.inflate(layoutInflater)
        setContentView(binding.root)
        setOnClickListeners();
    }

    private fun setOnClickListeners() {
        binding.apply {
            initiateId.setOnClickListener {
                // Inject the component into the activity
                val comp = DaggerTelivisionComponent.builder().build()
                comp.inject(this@DaggerMultiBindingIntoSetActivity)
                // Print the entire set
                println(telivision)
                // Iterate the set of objects
                telivision.forEach { tv ->
                    if(tv is Samsung){
                        // Access Samsung specific functionalities
                        println(tv.name())
                    }else if(tv is Lg){
                        // Access Lg specific functionalities
                        println(tv.name())
                    }
                }
            }
        }
    }

}

Clone this wiki locally