-
-
Notifications
You must be signed in to change notification settings - Fork 719
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
View model injection #37
Comments
Ok. Let me some time to check it 👍 |
In a Fragment (support v4), you can already do Koin must not interfere with Lifecycle architecture and must be kept as an instance factory. To keep things simple, I will provide a lazy way of injecting your ViewModel from fragments/activity. |
What I've started to do in my code with v0.8.0 for lazy creation is to use the following (with same for
Then to use in an activity I'm writing |
That's perfectly right :) It will be included in 0.8.1 👍 |
Hi. I like where v0.8.0 is going with adding an API for view model injection. However, there are some things I noted.
I cannot inject the view model in the normal manner
I have to manually pull the activity or parent fragment in order to use shared view models.
For an activity, it is straightforward. However, for a parent fragment it is more complicated.
Configuring the koin context with
viewModel {}
makes it look like nested contexts should inject/get the same instance of the view model as the parent context. This could be confusing to people who have not read the underlying code.Presently, I have a different approach which hides the ViewModelProvider behind the koin context and allows for lazy injection.
This approach does require the context to be initiated in Fragment::onCreate or Activity::onCreate. Also, the ViewModels are KoinComponents eliminating constructor injection and negating the need for providing a custom ViewModelFactory.
The text was updated successfully, but these errors were encountered: