Reputation: 1427
I'm attempting to get a ViewModel scoped to a navigation graph and am not able to get NavController.getViewModelStoreOwner(id)
or the delegate by navGraphViewModels(id)
to resolve. I must be missing a dependency but haven't been able to find the correct one.
build.gradle:
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.0'
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.fragment:fragment:1.2.5'
implementation 'androidx.fragment:fragment-ktx:1.2.5'
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.2.0'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.2.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel:2.2.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.firebase:firebase-analytics:17.4.3'
implementation 'com.google.firebase:firebase-crashlytics:17.0.1'
implementation 'com.google.firebase:firebase-perf:19.0.7'
implementation 'com.google.firebase:firebase-messaging:20.2.0'
implementation 'com.jakewharton.timber:timber:4.7.1'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'android.arch.navigation:navigation-runtime:2.2.0'
implementation 'android.arch.navigation:navigation-fragment:2.2.0'
implementation 'android.arch.navigation:navigation-ui:2.2.0'
implementation 'android.arch.navigation:navigation-runtime-ktx:2.2.0'
implementation 'android.arch.navigation:navigation-fragment-ktx:2.2.0'
implementation 'android.arch.navigation:navigation-ui-ktx:2.2.0'
testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
MainActivity.kt
package com.cren90.sandbox
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.navigation.findNavController
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val navController = findNavController(R.id.nav_host_fragment)
navController.getViewModelStoreOwner(R.id.nav_graph) // getViewModelStoreOwner is not resolving
}
}
Upvotes: 0
Views: 240
Reputation: 199984
All of your navigation dependencies are using android.arch.navigation
, meaning you are only getting the 1.0.0
version of the pre-AndroidX Navigation.
They need to be androidx.navigation
if you want to use the AndroidX versions.
// This includes navigation-fragment and navigation-runtime-ktx already
implementation 'androidx.navigation:navigation-fragment-ktx:2.2.0'
// This includes navigation-ui already
implementation 'androidx.navigation:navigation-ui-ktx:2.2.0'
Upvotes: 1