-
Notifications
You must be signed in to change notification settings - Fork 2k
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
HiltViewModel does not support custom CreationExtras as it is not exposed to ViewModelComponentBuilder #3523
Comments
CreationExtras are generally not supposed to be for use inside the ViewModel. It is mostly a way to get needed objects to the factory. For example, if we added CreationExtras into the ViewModel today, that would immediately leak the activity/fragment because that is held in there as the SavedStateRegistryOwner. |
Well, yes, I don't need the CreationExtras in the ViewModel, I would need the CreationExtras in a provides function installed in the ViewModelComponent. 🤔 |
So how do I get custom CreationExtras to the factory, if not the ViewModel? |
The ViewModel and ViewModelComponent are basically equivalent in terms of leaking since with fastInit mode any Provider used in the ViewModel will leak the ViewModelComponent. At the moment, we're not looking to support getting CreationExtras into the ViewModel or ViewModelComponent. I assume your broader goal is something like this request for assisted injection of ViewModels #2287. I know that CreationExtras seems like a good way to do this, and it might be, but there are some questions I'm currently trying to discuss with the AndroidX team that owns ViewModels and CreationExtras to figure out what the story is here. |
In that case, is there any potential way to |
I'm sorry if I'm missing the point completely, but I thought the
And then in the |
CreationExtras is basically a So you could theoretically pass things from A to B by customizing the CreationExtras, and not by creating a custom assisted factory. |
So that's how it worked for me in my limited testing (only tested with activities, not fragments). The only thing that differs from what you'd get through the whole |
Hilt should be extended to provide first-class support for assisted ViewModel injection and My proposal: introduce a new Hilt annotation similar to |
This is now addressed by using assisted injection with ViewModels. Please check "Assisted Injection" section here. |
movieViewModel = new ViewModelProvider(
getViewModelStore(),
getDefaultViewModelProviderFactory(),
HiltViewModelExtensions.withCreationCallback(
getDefaultViewModelCreationExtras(),
(MyViewModel.Factory factory) -> factory.create(movieId)))
.get(MyInjectedViewModel.class); That's really cool. Thank you for adding this feature! |
Is there any way to do assisted injection by using |
Hi, @ken-kentan , you can pass the creation callback to the
And overloads for |
That works well! Thank you. |
Currently, ViewModelComponentBuilder is only able to create a SavedStateHandle for a ViewModel, but the idea of CreationExtras is to provide general purpose extras to the ViewModel on creation.
If the idea is that CreationExtras are part of public API and not just an internal helper for Google code, then it would make sense to make a CreationExtras available to ViewModelComponent, as even ViewModels that have no creation extras (old infra) could easily see an empty one.
dagger/java/dagger/hilt/android/internal/builders/ViewModelComponentBuilder.java
Line 27 in b5990a0
The text was updated successfully, but these errors were encountered: