Last active
January 9, 2020 22:26
-
-
Save truedem/e45bd7ea6fc1576b71bc79ea86bb4821 to your computer and use it in GitHub Desktop.
Android: Inject ViewModel in Fragment/Activity which has run-time dependencies
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Related: https://youtu.be/9fn5s8_CYJI (assisted injection in the end)