I have a `ScreenA` which is using `MyViewModel` i...
# compose
n
I have a
ScreenA
which is using
MyViewModel
instantiated using
hiltViewModel()
function.
ScreenA
calls
ScreenB
. I want to share
MyViewModel
between both screens. However, I’m popping
ScreenA
when I’m calling
ScreenB
. Is there a way to do this without create a custom
ViewModelStoreOwner
? 🤔 cc: @Ian Lake
j
You might wanna make ScreenA and ScreenB part of a nested navigation graph. They can then share a viewmodel by scoping it to the nested nav graph.
think smart 2
💡 1
n
Seems like a good idea… 🤔
j
Copy code
NavHost {
  navigation(route = "myNestedGraph") {
    composable { // ScreenA 
      val vm = hiltViewModel<MyViewModel>(viewModelStoreOwner = navController.getBackStackEntry("myNestedGraph"))
    }
    composable { // ScreenB
      // Get the vm in the same way.
    }
  }
}
n
It worked like a charm 😉 thanks @julioromano
👌 1