question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[AssistedInject] Integration with @HiltViewModel

See original GitHub issue

It’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:open
  • Created 3 years ago
  • Reactions:212
  • Comments:47 (9 by maintainers)

github_iconTop GitHub Comments

52reactions
bompfcommented, Mar 17, 2021

This absolutely should be supported. Injecting ViewModels/Presenters is one of the primary use cases of AssistedInjection on Android.

46reactions
tfcporciunculacommented, Feb 2, 2021

@wbervoets With the new @HiltViewModel you can have the SavedStateHandle as a dependency without having to annotate it with @Assisted. The SavedStateHandle is now a binding from the new ViewModelComponent, so it doesn’t need to be assist-injected anymore.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found