2

I have created as asynctask inside a method. But it shows a warning.

This AsyncTask class should be static or leaks might occur (anonymous android.os.AsyncTask) less.. A static field will leak contexts. Non-static inner classes have an implicit reference to their outer class. If that outer class is for example a Fragment or Activity, then this reference means that the long-running handler/loader/task will hold a reference to the activity which prevents it from getting garbage collected. Similarly, direct field references to activities and fragments from these longer running instances can cause leaks. ViewModel classes should never point to Views or non-application Contexts.

How can I avoid this warning?

pirho
  • 11,565
  • 12
  • 43
  • 70
Nej
  • 127
  • 3
  • 13

1 Answers1

0

what this warning is trying to say, is that you can leak your activity/fragment in your phones memory.

if you create an AsyncTask that uses an activity context or the AsyncTask is and child of the activity/fragment, you keep the activity/fragment in memory. When you rotate or change activity in the periode the AsyncTask is running. you keep your old activity instance in memory and are leaking the activity/fragment.

quantum apps
  • 938
  • 2
  • 13
  • 25