79511596

Date: 2025-03-15 18:33:37
Score: 0.5
Natty:
Report link

The problem with this code was that in the screen composable I create the viewModel with the class constructor, like so:

@Composable
fun CameraScreen(
    modifier: Modifier = Modifier,
    lifecycleOwner: LifecycleOwner = LocalLifecycleOwner.current,
    cameraViewModel: CameraViewModel = CameraViewModel(),
)

Which doesn't make it part of the composition, nor does it save its state. To solve this I just changed the assignment of the default value as follows:

@Composable
fun CameraScreen(
    modifier: Modifier = Modifier,
    lifecycleOwner: LifecycleOwner = LocalLifecycleOwner.current,
    cameraViewModel: CameraViewModel = viewModel()
)

And now everything works correctly!

Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Angel