[AssistedInject] Integration with @HiltViewModel
See original GitHub issueIt’d be nice to have Assisted Injection support for Hilt ViewModels.
A nice API would be something like the following:
@HiltViewModel
class PlantDetailViewModel @AssistedInject constructor(
savedStateHandle: SavedStateHandle,
plantRepository: PlantRepository,
@Assisted private val plantId: String
) : ViewModel() { ... }
@AssistedFactory
interface PlantDetailViewModelFactory {
fun create(plantId: String): PlantDetailViewModel
}
And use it from the View like:
@AndroidEntryPoint
class PlantDetailFragment : Fragment() {
private val args: PlantDetailFragmentArgs by navArgs()
@Inject
lateinit var plantDetailViewModelFactory: PlantDetailViewModelFactory
private val plantDetailViewModel: PlantDetailViewModel by viewModels {
plantDetailViewModelFactory.create(args.plantId)
}
}
Issue Analytics
- State:
- Created 3 years ago
- Reactions:212
- Comments:47 (9 by maintainers)
Top Results From Across the Web
Providing AssistedInject supported ViewModel for ... - Medium
Hey Androiders , The hilt has reached stability and getting plenty of attention from the developers due to its simplicity and the feature ......
Read more >Use Hilt with other Jetpack libraries - Android Developers
Inject ViewModel objects with Hilt. Provide a ViewModel by annotating it with @HiltViewModel and using the @Inject annotation in the ViewModel ...
Read more >Ian Lake on Twitter: "@gvozditskiy @shakil807 @b95505017 ...
It'd be nice to have Assisted Injection support for Hilt ViewModels. A nice API would be something like the following: @HiltViewModel class PlantDetailViewModel ......
Read more >What's new in Hilt and Dagger 2.31 | by Jaewoong Eum
Each component has a corresponding scope that is integrated into the ... But in Hilt 2.31, HiltViewModel is newly introduced and we use...
Read more >Shared viewModel with assisted injection and hilt
@HiltViewModel class ViewModel @Inject constructor( private val repository, savedStateHandle: SavedStateHandle ) : ViewModel() { init ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This absolutely should be supported. Injecting ViewModels/Presenters is one of the primary use cases of AssistedInjection on Android.
@wbervoets With the new
@HiltViewModel
you can have theSavedStateHandle
as a dependency without having to annotate it with@Assisted
. TheSavedStateHandle
is now a binding from the newViewModelComponent
, so it doesn’t need to be assist-injected anymore.