Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inject ViewModel of activity to fragment using sharedViewModel creates new instance. #720

Closed
omercanyvision opened this issue Feb 18, 2020 · 1 comment
Labels
question Usage question

Comments

@omercanyvision
Copy link

I am trying to inject view model with dynamic parameters in it's constructor to fragment via sharedViewModel and new instance of this view model is created instead of using the instance that was created by the activity. If i am not passing parameters to shared view model the app crashes with
Could not create instance for [type:Factory,primary_type:'co.anyvision.abraxas.settings.padding.CameraPaddingViewModel']

To Reproduce
This is the view model module
val cameraPaddingViewModelModule = module { viewModel { (padding: Padding) -> CameraPaddingViewModel(padding) } }
This is the creation of view model in the activity
val viewModel: CameraPaddingViewModel by viewModel{ parametersOf(padding) }

now in fragment level:
if i try
val viewModel: CameraPaddingViewModel by sharedViewModel(from = { activity!! })
the app crashes with:
Could not create instance for [type:Factory,primary_type:'co.anyvision.abraxas.settings.padding.CameraPaddingViewModel']

if try with getSharedViewModel same crash happens. I can inject the view model to fragment by passing paramater as i did in the activity level but in that case koin inject new instance instead of the one i already injected to activity.

Koin versions:
koin-android-2.0.1
koin-viewmodels-2.0.1

@krasavello13
Copy link

krasavello13 commented Feb 28, 2020

Hi, try to update to 2.1.1 version of Koin and after use this solution:
#593

@arnaudgiuliani arnaudgiuliani added the question Usage question label Apr 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Usage question
Projects
None yet
Development

No branches or pull requests

3 participants