I'm using Hilt and MVVM at my project and I want to get an viewModel from activityViewModel to use same in 2 activites. But my Android Studio says Unresolved Reference.
My app build.gradle is like this:
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-kapt'
id 'dagger.hilt.android.plugin'
}
. . .
dependencies {
. . .
implementation "com.google.dagger:hilt-android:2.38.1"
kapt "com.google.dagger:hilt-android-compiler:2.38.1"
implementation 'androidx.hilt:hilt-navigation-fragment:1.0.0'
implementation 'androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03'
kapt 'androidx.hilt:hilt-compiler:1.0.0'
. . .
}
My project build.gradle file is like this:
classpath 'com.google.dagger:hilt-android-gradle-plugin:2.38.1'
I'm trying to get ViewModel like this:
private val viewModel: SelectWifiViewModel by activityViewModels()
activityViewModels is used to get a reference to the ViewModel scoped to its Activity inside a fragment. If you want to used same ViewModel in both the activities, I would suggest you to switch to Fragments instead.
activityViewModels() has nothing to do with Hilt
you need a different dependency for that. It is in
implementation "androidx.fragment:fragment-ktx:1.4.0"
by activityViewModels() is a Kotlin property delegate from the fragment-ktx library.
try to add this dependency:
implementation "androidx.navigation:navigation-fragment-ktx:2.3.5"