adrielcafe / voyager

🛸 A pragmatic navigation library for Jetpack Compose
https://voyager.adriel.cafe
MIT License
2.58k stars 136 forks source link

viewmodel not destroy in recomposition state #477

Open javad0470 opened 2 months ago

javad0470 commented 2 months ago

Hello,

I hope this message finds you well.

I'm currently using Voyager version 1.1.0-beta02 along with Koin for dependency injection. Following the sample provided, I’m defining ViewModels within my composable screens as shown below: val vm = koinScreenModel()

However, I've encountered an issue where, during each recomposition of the composable screen, everything gets destroyed except for the ViewModel. While this behavior might be useful for temporarily sharing a ViewModel content between two composable screens, it causes significant challenges in other scenarios. Unfortunately, this issue persists across previous versions as well.

I would greatly appreciate it if you could address this issue or provide guidance on how to resolve it in my project.

Thank you so much for your help.

cjrvdev commented 2 months ago

As far as I know (Im rather new in kotlin/compose), the idea behind viewmodel is to survive recomposition. That should be the intented behavior

Kiolk commented 3 days ago

I found that, if you not specify unique key for screen

override val key: ScreenKey = uniqueScreenKey

koinScreenModel() every time return the same view model, unique parameter of parametersOf doesn't guaranty that.

I think, need to update documentation to clarify that sample