Skip to content

Instantly share code, notes, and snippets.

@truedem
Last active January 9, 2020 22:26
Show Gist options
  • Save truedem/e45bd7ea6fc1576b71bc79ea86bb4821 to your computer and use it in GitHub Desktop.
Save truedem/e45bd7ea6fc1576b71bc79ea86bb4821 to your computer and use it in GitHub Desktop.
Android: Inject ViewModel in Fragment/Activity which has run-time dependencies
// https://stackoverflow.com/questions/57752301/dagger2-10-inject-viewmodel-in-fragment-activity-which-has-run-time-dependenci
class ProductViewModel(
val product: Product,
val resourceProvider: ResourceProvider,
val appUtils: AppUtils,
val alertManager: AlertManager
) {
// ...
}
// private val viewModel by viewModels { ProductViewModelFactory(product) }
class ProductViewModelFactory @Inject constructor(
val resourceProvider: ResourceProvider,
val appUtils: AppUtils,
val alertManager: AlertManager
) {
fun create(product: Product): ProductViewModel {
return ProductViewModel(product, resourceProvider, appUtils, alertManager)
}
}
@truedem
Copy link
Author

truedem commented Jan 9, 2020

Related: https://youtu.be/9fn5s8_CYJI (assisted injection in the end)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment