I am having a shared View Model for activity and it's fragment.
My view model need's argument to be passed when instantiating from the activity(onCreate
only once)
viewModel =ViewModelProviders.of(this,
NoteViewModelFactory(application!!,
uid = intent!!.getStringExtra("uid")!!))
.get(NoteViewModel::class.java)
But from fragment i don't need to pass the argument as i am sure the i have the argument's passed once.
viewModel = ViewModelProviders.of(activity!!).get(NoteViewModel::class.java)
In Koin
i tried doing below.
val noteModule = module(override = true) {
viewModel { (id: String) -> NoteViewModel(androidApplication(), id) }
}
in Activity:
private val viewModel: NoteViewModel by viewModel { parametersOf(intent!!.getStringExtra("uid")!!) }
in Fragment:
private val viewModel: NoteViewModel by sharedViewModel()
Application Crashed with below error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.andor.navigate.notepad/com.andor.navigate.notepad.listing.NotesActivity}: org.koin.core.error.InstanceCreationException: Could not create instance for [type:Factory,primary_type:'com.andor.navigate.notepad.core.NoteViewModel'] at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) Caused by: org.koin.core.error.InstanceCreationException: Could not create instance for [type:Factory,primary_type:'com.andor.navigate.notepad.core.NoteViewModel'] at org.koin.core.instance.DefinitionInstance.create(DefinitionInstance.kt:61) at org.koin.core.instance.FactoryDefinitionInstance.get(FactoryDefinitionInstance.kt:37) at org.koin.core.definition.BeanDefinition.resolveInstance(BeanDefinition.kt:70) at org.koin.core.scope.Scope.resolveInstance(Scope.kt:165)
I am not able to understand how to solve this using KOIN.
P.S:i am new to koin DI.