the above issue happens only on Android, while on iOS the Home screen is reinitialized everytime, navigation works correctly, but the screen has to reload everything again (even the ViewModel
init
is called)
m
Meet
09/29/2024, 7:46 AM
i have same issue i solve this problem using koin
Implement ViewModel Modules using Expect/Actual
ViewModelModule.android.kt
use viewmodel and other platform use factory
Notes: It is possible to directly initialize the ViewModel in the
commonMain
source set. I tested this approach, and while it worked perfectly on Android, there were issues on iOS and Desktop. Specifically, when using nested NavGraph or nested NavHost, the ViewModel may be recreated unexpectedly without any error, leading to unexpected behavior. Additionally, avoid using singleton for ViewModels on iOS and Desktop, as the
ViewModelScope
is destroyed when using singletons.In the future, there may be official ViewModel support for
commonMain
, making this process more seamless.
r
Raed Ghazal
09/29/2024, 7:49 AM
hmm, yeah this can be related to koin actually 🤔
But
In the future, there may be official ViewModel support for
commonMain
, making this process more seamless.
isn't that already implemented? so compose added viewmodels in commonMain, and koin have a
viewModelOf
that we can now use in commonMain, apparently its buggy, but its supported