Mastering Hilt - Android | Kotlin | Hilt (Dagger 2) [ Part 4]
Becoming Proficient in Dependency Injection with Hilt for Android (Kotlin)
Table of contents
Constructor Injection in Hilt
Injecting Dependencies into
Activities
Injecting Dependencies into
Fragments
Injecting Dependencies into
ViewModels
Injecting Dependencies into Activities:
To inject dependencies into Activities, you can use the @Inject
annotation on the constructor of the Activity
. Hilt will automatically provide the required dependencies when the Activity
is created.
Example - Injecting a dependency into an Activity
:
@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
@Inject
lateinit var myDependency: MyDependency // Injected dependency
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Now, we can use myDependency in our Activity
// ...
}
}
In this example, the MainActivity
is marked with @AndroidEntryPoint
, enabling Hilt to perform dependency injection. The MyDependency
instance is annotated with @Inject
, indicating that it should be provided by Hilt when the MainActivity
is created. The myDependency
variable is then injected with the provided instance.
Injecting Dependencies into Fragments:
To inject dependencies into Fragments, you can use the @Inject
annotation on the constructor of the Fragment. Similar to Activities, Hilt will automatically provide the required dependencies when the Fragment is created.
Example - Injecting a dependency into a Fragment
:
@AndroidEntryPoint
class MyFragment : Fragment() {
@Inject
lateinit var myDependency: MyDependency // Injected dependency
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_my, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// Now, we can use myDependency in our Fragment
// ...
}
}
In this example, the MyFragment
is marked with @AndroidEntryPoint
, enabling Hilt to perform dependency injection. The MyDependency
instance is annotated with @Inject
, indicating that it should be provided by Hilt when the MyFragment
is created. The myDependency
variable is then injected with the provided instance.
Injecting Dependencies into ViewModels:
To inject dependencies into ViewModels, you can use the @ViewModelInject
annotation on the ViewModel's constructor. Hilt will automatically provide the required dependencies when creating the ViewModel.
Example - Injecting a dependency into a ViewModel
:
class MyViewModel @ViewModelInject constructor(
private val myDependency: MyDependency // Injected dependency
) : ViewModel() {
// Now, you can use myDependency in your ViewModel
// ...
}
In this example, the MyViewModel
is annotated with @ViewModelInject
, enabling Hilt to perform dependency injection. The MyDependency
instance is declared as a parameter in the ViewModel's constructor and annotated with @ViewModelInject
, indicating that it should be provided by Hilt when the ViewModel
is created.
By using constructor injection in Hilt, we can create cleaner, more maintainable code and ensure that our dependencies are automatically provided wherever they are needed in our Android app.