如何使用Android Studio中的ViewModel()使用HILT依赖性注入上下文?
我希望能与Hilt for ViewModel进行依赖注入上下文,
我已经阅读了文章 ,并基于代码A的文章。
但是我得到以下警告信息,为什么?我该如何修复?
此字段泄漏了一个上下文对象
btw,我读过 atrate ,我的项目是hilt_version ='2.41'
。
代码a
@HiltViewModel
class SoundViewModel @Inject constructor(
@ApplicationContext private val mContext: Context, //It cause a warning information
private val aSoundMeter: ISoundMeter
): ViewModel() {
}
I hope to dependency injection Context with Hilt for ViewModel,
I have read the article, and Code A based the article.
But I get the following warning information, why? How can I fix it?
This field leaks a context object
BTW, I have read the article, and my project is hilt_version = '2.41'
.
Code A
@HiltViewModel
class SoundViewModel @Inject constructor(
@ApplicationContext private val mContext: Context, //It cause a warning information
private val aSoundMeter: ISoundMeter
): ViewModel() {
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
AndroidViewModel
而不是ViewModel
Use
AndroidViewModel
instead ofViewModel
您必须遵循官方指南首先。
或者
You must follow the latest version of the official guide first.
or