1

My question is similar with this question here, which in the accepted answer the issue already solved in version 2.31.2. But the warning still exists for me and im using version 2.37. The other answers is still unclear for me (not actually confirmed what is happening). So this is my ViewModel code:

@HiltViewModel
class DetailViewModel @Inject constructor(
    private val context: Context,
    private val repository: VaccineRepository,
) : ViewModel() {

And this is the provides function:

@Singleton
@Provides
fun provideApplicationContext(
    @ApplicationContext appContext: Context
): Context = appContext

It does works but the warrning is bothering me. So, is it just false warning and i can just suppress it or is it actually leaks?

wiryadev
  • 1,089
  • 1
  • 11
  • 33

1 Answers1

5

Since ViewModels are meant to survive across activity recreations storing an activity context there would leak your context (due to the nature of activity which destroys easily). But application context is unique across the app until the whole application gets killed, so there would not be any actual leak and you can ignore the warning by adding @SuppressLint("StaticFieldLeak") to your DetailViewModel class.

The inspection should have considered your @ApplicationContext annotation and come up with a meaningful outcome but it didn't.

Sdghasemi
  • 5,370
  • 1
  • 34
  • 42