1

So, I've got a couple of warnings: 1. Do not place Android context classes in static fields; this is a memory leak 2. This 'AsyncTask' class should be static or leaks might occur.

If I remove static from the field, then my static function, itself called from a statically declared AsyncTask gives a failure. If I make the other AsyncTask static then I go chasing my tail making other functions and fields static (or at least I initially started to before reverting).

I did see in another answer (Warning: This AsyncTask class should be static or leaks might occur) for consideration that since the AsyncTask should be short-lived, not to worry about making them static.

I think I understand the difference between static and not, but how should I make the decision for what should/should not be? Have I missed something fundamental about how to interact with the AsyncTaks and using variables?

PeteC
  • 194
  • 1
  • 9
  • 1
    Note that `AsyncTask` is deprecated. In general, the only non-constant objects that should be `static` are objects that, by design, need to live for the lifetime of the process. So, for example, a memory cache of data that you load from disk or the network typically winds up either being `static` or being managed by something that itself is `static`. In modern app development, usually libraries are handling our `static` data for us (e.g., Glide with an image cache, Dagger/Koin for dependency inversion). – CommonsWare May 23 '20 at 18:50

0 Answers0