android : @AndroidEntryPoint and @HiltViewModel usage
@AndroidEntryPoint is an annotation used in Hilt, which is Google’s recommended library for Dependency Injection (DI) built on top of Dagger.
What does it actually do?
When you mark a class with @AndroidEntryPoint, Hilt generates an individual Hilt component for that specific Android class (in this case, your MainActivity).
Member Injection: It allows the class to receive dependencies from Hilt. Without this, you couldn't use the @Inject annotation to get your ViewModels, Repositories, or API services into the Activity.
Lifecycle Management: It ensures that the dependencies are tied to the Activity's lifecycle. Hilt will automatically handle creating the component in onCreate() and destroying it when the Activity is destroyed.
There are a few things to remember how to use this. We need to use viewModel delegate
@dagger.hilt.android.AndroidEntryPoint
class MainActivity : ComponentActivity() {
private val viewModel: HomeViewModel by viewModels()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContent {
GetMyHomeTheme {
GetMyHomeApp(viewModel = viewModel)
}
}
}
}
How the "Chain" works
When you call by viewModels(), Hilt does the following behind the scenes:
Lookups: It looks at the
HomeViewModelconstructor.Fulfillment: It sees it needs
PropertySearchBackendApiandOkHttpClient.Search: It searches your Hilt Modules (classes marked with
@Module) to find instructions on how to create those specific objects.Delivery: It creates the objects, creates the ViewModel, and hands it to your Activity.
@HiltViewModel // <--- Use this for ViewModels
class HomeViewModel @Inject constructor(
private val propertySearchBackendApi: PropertySearchBackendApi,
private val graphQLClient: OkHttpClient
) : ViewModel() { ... }
Comments