Middle
Ako pridať kontext do metódy pomocou Dagger Hilt?
sobes.tech AI
Odpoveď od AI
Injektujte Context pomocou @ApplicationContext alebo @ActivityContext.
class MyViewModel @Inject constructor(
@ApplicationContext private val context: Context // Injektovanie kontextu aplikácie
) : ViewModel() {
// Použitie kontextu
}
class MyFragment @Inject constructor() : Fragment() {
@Inject
lateinit var myPresenter: MyPresenter // Injektovanie prezentéra, ktorý používa kontext aktivity
override fun onAttach(context: Context) {
super.onAttach(context)
// Dagger Hilt vykoná injektáž
}
}
class MyPresenter @Inject constructor(
@ActivityContext private val context: Context // Injektovanie kontextu aktivity
) {
// Použitie kontextu
}