Is anyone super familiar with hiltViewModel() and ...
# compose
c
Is anyone super familiar with hiltViewModel() and compose nav. Starting to go crazy on this error that it can't create a VM. Am I doing something obvioiusly wrong here?
Copy code
bottomSheet(route = Screen.DebugBottomSheet.route) {
  DebugBottomSheet(previousScreenVM = hiltViewModel(remember { navController.previousBackStackEntry!! }))
}
my bottomsheet
Copy code
@Composable
fun DebugBottomSheet(
    viewModel: DebugBottomSheetViewModel = hiltViewModel(),
    previousScreenVM: ViewModel,
) {}
Error:
java.lang.RuntimeException: Cannot create an instance of class androidx.lifecycle.ViewModel
a
You need to specify the class of your view model:
hiltViewModel<MyViewModel>()
.
c
Is there some way to just get whatever ViewModel is there for the previousBackStackEntry? I'm assuming not... but I figured id ask. back to square 1 it seems.
j
What are you trying to achieve with that?
c
I have a "debug" panel that can essentially draw over top of any of my screens. I want to be able to call certain methods inside of these view models (a la contextual actions from JakeWhartons u2020 debug drawer) https://github.com/JakeWharton/u2020
Another potential use case I could come up with though is if I have a common ViewModel. Lets call it MyBaseVM, and I want hiltViewModel to always give me any VM as long as it's MyBaseVM that I use on my screens because there is some common method i want to call.