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

Colton Idle

06/07/2022, 7:50 PM
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

Stylianos Gakis

06/07/2022, 9:38 PM
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

Francesc

06/07/2022, 9:45 PM
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

Stylianos Gakis

06/08/2022, 5:06 AM
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

myanmarking

06/08/2022, 8:28 AM
if remember doesn’t work, the problem is probably not the viewModel
a

allan.conda

06/08/2022, 2:39 PM
Only this VM specifically right? It might be the Scaffold as recomposition can be sometimes weird within that component😄
m

myanmarking

06/09/2022, 8:48 AM
idk. it is very weird. remember should work no matter how bad you screw up. Maybe you have two viewmodel instances somehow ? no idea
3 Views