Skip to content

Latest commit

 

History

History
32 lines (26 loc) · 667 Bytes

part-4.md

File metadata and controls

32 lines (26 loc) · 667 Bytes

Part 4 - Injecting ViewModel in activity.

Part 3

Dependency

Add the below dependencies in app level build.gradle along with the dependencies in Part 1.

dependencies {
    //...
    implementation 'androidx.activity:activity-ktx:1.4.0'
}

Annotate the ViewModel class

@HiltViewModel
class ActivityMainViewModel : ViewModel() {
    // ...
}

Use the ViewModel class in activity

import androidx.activity.viewModels

@AndroidEntryPoint
class ActivityMain: AppCompatActivity() {
    private val activityMainViewModel: ActivityMainViewModel by viewModels()
    
    // ...
}