来自article https://developer.android.com/training/dependency-injection/hilt-android#component-default,我知道每个 Hilt 组件都带有一组默认绑定,Hilt 可以将它们作为依赖项注入。你可以看到图像A。
And the ViewModelComponent
只有一个默认绑定SavedStateHandle
根据上面的文章。
为什么我能够使用Application
作为默认绑定ViewModelComponent
与代码 A 中的希尔特?
@HiltViewModel
class PlaySoundViewModel @Inject constructor(
private val appContext: Application, // It's Ok, I have tested.
private val playSound: IPlaySound,
private val savedStateHandle: SavedStateHandle
): ViewModel(){
...
}
Image A
根据此图(https://developer.android.com/training/dependency-injection/hilt-android#component-hierarchy https://developer.android.com/training/dependency-injection/hilt-android#component-hierarchy) ,我们可以访问父组件的绑定,正如您所提到的SingletonComponent
have Application
作为默认绑定,ViewModel
能够访问Application
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)