Last active
July 6, 2020 21:09
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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