-
-
Notifications
You must be signed in to change notification settings - Fork 721
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
Cannot resolve viewModel injection with delegate style declaration #56
Comments
Your code looks good to me. // Koin for Android Architecture Components
compile "org.koin:koin-android-architecture:0.8.2" |
Are you importing correctly?
|
Maybe try declaring the type in the value?
|
You don't need the angle brackets for the viewmodel I believe |
Scratch that I misunderstood that your activity was generic |
What about
|
Hello, we can't use generic type Best thing is to add a method to allow such declaration: |
What I trying to do is receive the type of View Model as parameter and inject a new instance of this type. There's no MyViewModel. The ViewModel types would be received in a generic way. The problem is because it's an inline function with reified type, it's not allowed to receive the type like we have here. It would be amazing to be able to use like this: I'm going to try to strip the injection source code and implement a ViewModelFactory to initialize it inside the BaseActivity but this would not benefit from the rest of the library features |
After 2 days I had no success trying to make this work. |
Does
Not work? |
Yes Ok. Sorry there was a mistake in my answer.
Let me write a patch 👍 |
fix is available in current alpha: |
API has been updated to have Your base activity must be like: class Base Activity<T : ViewModel>(clazz : KClass<T>){
val model : T by viewModel(clazz)
} We can't pass reified parameters from class. |
It worked! Thanks very much!!! I'll try make an extension or something like that to avoid having to pass the class as a parameter but for now, that's working wonderfully!! |
Cool :) I close the issue. |
@shrpereira Did you manage to avoid having to pass the class as a parameter? |
@shrpereira You will be able to get viewModel by class with |
@shrpereira Have you added an extension to not pass class as a parameter to the activity ? |
@fathallah92
But I'm not coding on this project for some time (it's in production, though), so, probably there are better ways to do it. |
@arnaudgiuliani |
Hope this isn't too late. You can try this:
|
There is a problem when using databinding at the same time. |
@nEdAy if use ViewDataBinding use need change 0 to 1
|
Is there an example of how this is implemented on a example Fragment? I am getting a No definition found error when I use the above example. The only way I have gotten it to work was going around the lazy and doing a loadKoinModules(module{single{ExampleViewModel()}}) inside the ExampleFragment which kind of defeats the purpose. |
I have figured out by myself, quite similar with your approach , thank you for answer, happy coding :D |
I did it like this
|
I'm trying to follow the documentation using delegate style declaration of ViewModels but I cannot resolve the import in Android Studio.
This is my code:
What I'm doing wrong? Any advice is appreciated.
The text was updated successfully, but these errors were encountered: