Sobes.tech
Middle

Dagger Hilt yordamida metodga kontekst qanday qo'shish mumkin?

sobes.tech AI

AIdan javob

@ApplicationContext ёки @ActivityContext орқали Contextни инжекторлаш.

class MyViewModel @Inject constructor(
    @ApplicationContext private val context: Context // Тасвирланган контекстни инжекторлаш
) : ViewModel() {
    // Контекстдан фойдаланиш
}
class MyFragment @Inject constructor() : Fragment() {

    @Inject
    lateinit var myPresenter: MyPresenter // Activity контекстига тааллуқли презентерни инжекторлаш

    override fun onAttach(context: Context) {
        super.onAttach(context)
        // Dagger Hilt инжекцияни амалга оширади
    }
}

class MyPresenter @Inject constructor(
    @ActivityContext private val context: Context // Activity контекстига инжекторлаш
) {
    // Контекстдан фойдаланиш
}