Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Asynctask: Is inner class necessary?

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.)

like image 860
soonoo Avatar asked Jan 15 '15 06:01

soonoo


People also ask

Why do we need inner 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.

Why is AsyncTask deprecated?

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.

What is the purpose of the AsyncTask class in Android?

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).


3 Answers

'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:

  1. You create the Asynctask as an inner class to your activity and it is off doing something time consuming. Inner classes by definition hold a reference to their 'outer' parent (this is how they access their parent's data).
  2. Android shuts down the activity and restarts a new activity (for example when you rotate the phone).
  3. Now the old activity and all it's resources are still being held because the Asynctask is still running and holding a reference to your old activity; so suddenly you are using memory for the old version of the activity AND the new version of the activity.

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.

like image 138
Elemental Avatar answered Oct 04 '22 13:10

Elemental


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.

like image 31
Ofek Agmon Avatar answered Oct 04 '22 12:10

Ofek Agmon


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.

like image 32
marcinj Avatar answered Oct 04 '22 11:10

marcinj