8

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.

T D Nguyen
  • 7,054
  • 4
  • 51
  • 71

1 Answers1

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

rooest
  • 167
  • 1
  • 9