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:

  1. Lookups: It looks at the HomeViewModel constructor.

  2. Fulfillment: It sees it needs PropertySearchBackendApi and OkHttpClient.

  3. Search: It searches your Hilt Modules (classes marked with @Module) to find instructions on how to create those specific objects.

  4. 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

Popular posts from this blog

vllm : Failed to infer device type

android studio kotlin source is null error

gemini cli getting file not defined error