Skip to content

Instantly share code, notes, and snippets.

@mitchtabian
Last active July 6, 2020 21:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mitchtabian/130b29cf33696fc02710df3f7aba8152 to your computer and use it in GitHub Desktop.
Save mitchtabian/130b29cf33696fc02710df3f7aba8152 to your computer and use it in GitHub Desktop.
Basics #2: Field injecting a class with no dependencies. You own that class.
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject
@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
@Inject
lateinit var someClass: SomeClass
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
println(someClass.doAThing())
println(someClass.doSomeOtherThing())
}
}
class SomeClass
@Inject
constructor(
private val someOtherClass: SomeOtherClass
){
fun doAThing(): String{
return "Look I did a thing!"
}
fun doSomeOtherThing(): String{
return someOtherClass.doSomeOtherThing()
}
}
class SomeOtherClass
@Inject
constructor(
){
fun doSomeOtherThing(): String{
return "Look I did some other thing!"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment