https://kotlinlang.org logo
#compose
Title
# compose
c

Colton Idle

01/19/2022, 10:03 PM
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

Albert Chang

01/20/2022, 3:40 AM
You need to specify the class of your view model:
hiltViewModel<MyViewModel>()
.
c

Colton Idle

01/20/2022, 3:49 AM
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

jossiwolf

01/20/2022, 9:19 AM
What are you trying to achieve with that?
c

Colton Idle

01/20/2022, 7:09 PM
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.
4 Views