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 {
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.
source share