Middle
Jak přidat kontext do metody pomocí Dagger Hilt?
sobes.tech AI
Odpověď od AI
Injektovat Context pomocí @ApplicationContext nebo @ActivityContext.
class MyViewModel @Inject constructor(
@ApplicationContext private val context: Context // Injekce kontextu aplikace
) : ViewModel() {
// Použití kontextu
}
class MyFragment @Inject constructor() : Fragment() {
@Inject
lateinit var myPresenter: MyPresenter // Injekce presenteru, který používá kontext aktivity
override fun onAttach(context: Context) {
super.onAttach(context)
// Dagger Hilt provede injekci
}
}
class MyPresenter @Inject constructor(
@ActivityContext private val context: Context // Injekce kontextu aktivity
) {
// Použití kontextu
}