Android Asynctask: inner class needed?

I tried connecting to the network in Android using Asynctask and succeeded better or not.

What interests me is: do I need to make an Asynctask inner class?

I did not do this as a form of the inner class, but the Google API AsyncTask must be subclassed to be used. says AsyncTask must be subclassed to be used. (But I did not do this and it works ...) - http://developer.android.com/reference/android/os/AsyncTask.html

And why does Asynctask take the form of a class, although it only has one core function? (I think it should be a method, not a class.)

+5
source share
3 answers

"AsyncTask must be a subclass to use" does not mean an inner class; this means that the class extends AsyncTask.

Typically, AVOID users use ASyncTask as (non-static) inner classes, because there is a big and subtle issue that looks something like this:

  • You create Asynctask as an inner class for your activity and do not execute much time. Inner classes, by definition, contain a link to their "external" parent element (this way they access the parent data).
  • Android disables activity and restarts new activity (for example, when the phone is turned).
  • Now the old activity and all its resources are still stored, because Asynctask is still working and contains a link to your old activity; so suddenly you are using memory for the old version of the activity and the new version of this action.

This difficulty is most easily solved with the help of a static inner class (which cannot refer to its outer parent element) or another class and refers only to the action using weak_reference.

+13
source

It does not have to be an inner class, it depends on what you use it for.

for example, if you have AsyncTask as an inner class, only its class can use it, but you have AsyncTask that uses many classes, you can put AsyncTask in an open class, everyone can use it.

It can work both ways.

+4
source

What interests me is: do I need to make an Asynctask inner class?

No, you can put it in an external java file, or you can also make it an internal static class.

AsyncTask must be subclassed for use.

this has nothing to do with the fact that the class is internal, it says that you need to create a subclass of AsyncTask, i.e.:

 class MyAsyncTask extends AsyncTask 
Inner class

as follows:

 class MyActivity extends Activity { // now MyAsyncTask is inner to MyActivity and has full access to its instance class MyAsyncTask extends AsyncTask {} } 

And why does Asynctask take the form of a class, although it only has one core function? (I think it should be a method, not a class.)

it does a lot more, for example onPostExecute, onPreExecute, looks at it closer. It also separates AsyncTask logic and allows reuse.

If you want AsyncTask to be internal, I suggest you make it static - but then why not make it an external class. The inner class always refers to its outer class, so in this case the Activity will not be garbage collected until AsyncTask finishes its work (the thread ends), since network communication takes time, this can cause problems - for example, leaks links or OOM (Out Of Memory) if your activity uses a lot of memory. It’s better to keep a link to your activity in WeakReference to allow the Activity to be collected by garbace.

+3
source

Source: https://habr.com/ru/post/1211134/


All Articles