the above issue happens only on Android, while on ...
# compose
r
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
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
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
wait a sec, I'm using
Copy code
org.jetbrains.androidx.lifecycle:lifecycle-viewmodel
while in the documentation they say we need to add
Copy code
org.jetbrains.androidx.lifecycle:lifecycle-viewmodel-compose
let me try that
nah didn't change anything, probably its koin issue
m
i am not sure this is not koin issue this lifecycle issue
r
yeah.. I used your solution with
factoryOf
in IOS, didn't work