I have a ViewModel
which has a dependency which should be taken from the Fragment
's arguments
.
So its something like:
class SomeViewModel(someValue: SomeValue)
now the fragment recieves the SomeValue
in its arguemnt like so:
class SomeFragment : Fragment() {
val someViewModel: SomeViewModel by viewModel()
companion object {
fun newInstance(someValue: SomeValue) = SomeFragment().apply {
arguments = bundleof("someKey" to someValue)
}
}
}
problem is I don't know how to add SomeValue
thats taken from the Fragment
's arguments
to Koin
's module.
Is there a way to make the fragment contribute to the Koin Dependency Graph?
So for anyone else asking the same question, here is the answer:
https://doc.insert-koin.io/#/koin-core/injection-parameters
So basically,
you could create your module like so:
val myModule = module {
viewModel { (someValue : SomeValue) -> SomeViewModel(someValue ) }
}
Now in your fragment, you could do something like:
class SomeFragment : Fragment() {
val someViewModel: SomeViewModel by viewModel {
parametersOf(argument!!.getParcelable<SomeValue>("someKey"))
}
companion object {
fun newInstance(someValue: SomeValue) = SomeFragment().apply {
arguments = bundleof("someKey" to someValue)
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.