So, I am using Koin
for dependency injection, Here is what I did inside a activity
class ModuleDetailActivity : AppCompatActivity() {
private lateinit var moduleId:String
private lateinit var levelModule:Level.Module
private val moduleViewModel: ModuleViewModel by viewModel { parameterOf(moduleId, levelModule) }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
...
...
moduleId = intent.getString("module_id")
levelModule = intent.getParcelable("level_module")
...
...
}
}
Now, I have multiple fragment which ModuleDetailActivity
can add or replace and I want the same instance of moduleViewModel
in those fragments without passing any parameters inside Fragment
.
class ModuleDetailFragment : Fragment() {
private val moduleViewModel: ModuleViewModel by sharedViewModel()
...
...
}
I know this will throw a error and as expected you can see this
Caused by: org.koin.core.error.InstanceCreationException: Could not create instance for [Factory:'****.ui.module.ModuleViewModel']
This is how I have initialized the module
val viewModelModule = module {
viewModel { (id : String, levelModule:Level.Module) -> ModuleViewModel(id, levelModule, get()) }
}
Is there any solution on how I can get the same instance of ModuleViewModel
defined inside activity without passing parameter inside a Fragment
?
ViewModel
instance withviewModel
. But what I want is the same instance which if defined in activity with parameter in fragment without parameters. – Spiky