https://kotlinlang.org logo
#koin
Title
# koin
m

masteramyx

09/29/2023, 9:27 PM
Hello! I'm having trouble getting a shared view model instance. VM is defined as
Copy code
module = {        viewModel { SharedViewModel(
            SearchProductsUseCase())
 }
    }
In my activity (Java; unfortunetly it needs to remain java right now.)
Copy code
SharedViewModel sharedViewModel = get(SharedViewModel.class);
In my fragment
Copy code
private val sharedViewModel: SharedViewModel by activityViewModel<SharedViewModel>()
But my logs show..
Copy code
activity VM INSTANCE: com.package.SharedViewModel@8e410b3
frag VM INTANCE: com.package.SharedViewModel@5d570c3
Why am I getting 2 different instances? Does it have to do with java class?
a

arnaud.giuliani

10/03/2023, 4:36 PM
yes I believe the java access is not good
SharedViewModel sharedViewModel = get(SharedViewModel.class);
you need to use
koin-android-compat
for Java
image.png
use
viewModel(this,SharedViewModel.class)
thank you color 1