Middle
Kaip pridėti kontekstą į metodą naudojant Dagger Hilt?
sobes.tech AI
Atsakymas iš AI
Injektuokite Context per @ApplicationContext arba @ActivityContext.
class MyViewModel @Inject constructor(
@ApplicationContext private val context: Context // Programos konteksto injekcija
) : ViewModel() {
// Konteksto naudojimas
}
class MyFragment @Inject constructor() : Fragment() {
@Inject
lateinit var myPresenter: MyPresenter // Injekcija, naudojanti Activity kontekstą
override fun onAttach(context: Context) {
super.onAttach(context)
// Dagger Hilt atliks injekciją
}
}
class MyPresenter @Inject constructor(
@ActivityContext private val context: Context // Activity konteksto injekcija
) {
// Konteksto naudojimas
}