'Hilt Inject field into any class with no scope
Suppose that I have a class A
class A ()
I want to inject an instance of A
as a field into class B
and let Hilt or Dagger to handle it.
Class B {
@Inject lateinit var a: A
}
Let say class B is a plain class, has no context, e.g viewmodel or anything, what are the proper steps (if possible) so I can use the instance a
without manual init.
Solution 1:[1]
If you can access constructor of class B you can pass as an argument like that:
class B @Inject constructor(
private val classA : A
){
//...
}
Otherwise you can use @EntryPoints
.
To learn more details about @EntryPoint
, you can click the link below:
https://developer.android.com/training/dependency-injection/hilt-android#not-supported
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | rooest |