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() {
}
CodePudding user response:
Use AndroidViewModel
instead of ViewModel
class SoundViewModel @Inject constructor(
@ApplicationContext private val mContext: Context,
private val aSoundMeter: ISoundMeter
): AndroidViewModel(mContext)
CodePudding user response:
You must follow the latest version of the official guide first.
@HiltViewModel
class ExampleViewModel @Inject constructor(
private val application: Application,
) : ViewModel() {
...
}
or
@HiltViewModel
class ExampleViewModel @Inject constructor(
@ApplicationContext private val mContext: Context,
) : ViewModel() {
...
}