I tried network connection in Android using Asynctask and succeeded better or not.
What I'm curious about: Is it necessary to make Asynctask inner class?
I didn't make it as a form of inner class, but Google API guide says AsyncTask must be subclassed to be used.
(But I didn't and It works...) - http://developer.android.com/reference/android/os/AsyncTask.html
And why Asynctask takes a form of class even though it performs a only one main function? (I think it should be a method, not a class.)
Inner classes are a security mechanism in Java. We know a class cannot be associated with the access modifier private, but if we have the class as a member of other class, then the inner class can be made private. And this is also used to access the private members of a class.
This class was deprecated in API level 30. AsyncTask was intended to enable proper and easy use of the UI thread. However, the most common use case was for integrating into UI, and that would cause Context leaks, missed callbacks, or crashes on configuration changes.
In Android, AsyncTask (Asynchronous Task) allows us to run the instruction in the background and then synchronize again with our main thread. This class will override at least one method i.e doInBackground(Params) and most often will override second method onPostExecute(Result).
'AsyncTask must be subclassed to be used' doesn't mean inner class; it means the class extends AsyncTask.
Generally people AVOID using ASyncTask as a (non-static) inner classes because there is a big and subtle problem which goes something like this:
This difficulty is most easily solved by using a static inner class (which cannot reference it's outer parent) or a different class and only reference the activity using a weak_reference.
It doesn't have to be a inner class, it depends on what you are using it for.
for example, if you have a AsyncTask as inner class, only the class that holds it can use it, but you have a AsyncTask that a lot of classes are using, you may want to put the AsyncTask as a public class so everyone can use it.
it can work in both ways.
What I'm curious about: Is it necessary to make Asynctask inner class?
No, you can put it to external java file, or you can also make it inner static class.
AsyncTask must be subclassed to be used.
it has nothing to do with class being inner, it says you need to make a subclass of AsyncTask ie:
class MyAsyncTask extends AsyncTask
inner class looks like that:
class MyActivity extends Activity {
// now MyAsyncTask is inner to MyActivity and has full access to its instance
class MyAsyncTask extends AsyncTask
{}
}
And why Asynctask takes a form of class even though it performs a only one main function? (I think it should be a method, not a class.)
it perform a lot more, like onPostExecute, onPreExecute, look at it closer. Also it decouples AsyncTask logic and allows for reuse.
If you want AsyncTask to be inner, then I suggest to make it static - but then why not make it external class. Inner class always keep reference to its external class, this way in this case Activity will not be garbage collected untill AsyncTask finishes its job (thread ends), because network communication takes time this might cause problems - like leaked references, or OOM (Out Of Memory) exceptions if your activity uses lots of memory. Its better to keep reference to your Activity in WeakReference to allow Activity to be garbace collected.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With