My newVm keeps getting it's `init {}` block. My fi...
# compose
c
My newVm keeps getting it's
init {}
block. My first two thoughts were to either use a
remember {}
or a LaunchedEffect. But both don't work. Any ideas?
Copy code
BottomSheetScaffold(
    scaffoldState = complexBottomSheetState,
    sheetContent = {
          Box(Modifier.fillMaxWidth().height(200.dp)
          ) {
            Log.e("ABC", "creating new VM?")
            val newVm = hiltViewModel<ComplexBottomSheetViewModel>()
            MyComplexBottomSheet(viewModel = newVm)
          }
    },
s
I'd check what hiltViewModel does. If it were a simple function that returns an object it makes sense that it gets called repeatedly since there's nothing remembering it across recompositions.
f
the documentation reads "returns an existing viewmodel or creates a new one" so it should return the same instance each time as long as this block is in the composition
s
Yeah I also see that in here it’s also used without remember or something like that. Not sure what it could be then.
m
if remember doesn’t work, the problem is probably not the viewModel
a
Only this VM specifically right? It might be the Scaffold as recomposition can be sometimes weird within that component😄
m
idk. it is very weird. remember should work no matter how bad you screw up. Maybe you have two viewmodel instances somehow ? no idea