I am new to Android programming. I would like to know if it is better to have seperate AsyncTasks for every screen on your application or is it better to have one AsynTask class that handles all processing from different screens on my application ?
I have looked at stackoverflow but its still not clear to me. I'm thinking one class might be a lot of overhead and making seperate AsyncTasks for every screen of my application will be good for performance.
On Tue, Nov 13, 2012 at 3:44 AM, Albertus Kruger
<albertus.kru...@gmail.com>wrote:
> I would like to know if it is better to have seperate AsyncTasks for every
> screen on your application or is it better to have one AsynTask class that
> handles all processing from different screens on my application ?
One class that handles everything seems like a terrible idea. An AsyncTask
should represent a single task. It does one thing, reports progress on it,
and returns a result. That's it.
--------------------------------------------------------------------------- ----------------------
TreKing <http://sites.google.com/site/rezmobileapps/treking> - Chicago
transit tracking app for Android-powered devices
Have a separate Async task class which serves the HTTP Request to all of
your Activities and let your Activities implments a listener which helps
the AsycnTask class to get the data from "onpostexecutes" method.
here is an example which i have written in a blog.
On Wed, Nov 14, 2012 at 3:41 PM, TreKing <treking...@gmail.com> wrote:
> On Tue, Nov 13, 2012 at 3:44 AM, Albertus Kruger <
> albertus.kru...@gmail.com> wrote:
>> I would like to know if it is better to have seperate AsyncTasks for
>> every screen on your application or is it better to have one AsynTask class
>> that handles all processing from different screens on my application ?
> One class that handles everything seems like a terrible idea. An AsyncTask
> should represent a single task. It does one thing, reports progress on it,
> and returns a result. That's it.
> --------------------------------------------------------------------------- ----------------------
> TreKing <http://sites.google.com/site/rezmobileapps/treking> - Chicago
> transit tracking app for Android-powered devices
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to android-developers@googlegroups.com
> To unsubscribe from this group, send email to
> android-developers+unsubscribe@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en
Create a separate AsyncTask subclass for each different task that needs to be done.
It is bad Java design to create one large AsyncTask subclass that handles everything in your app. That would mean you'd have to create a large 'doInBackground' method (being able to handle all cases), possibly create a large 'onPostExecute' as well, etc, and riddle your code with 'if' statements or large 'switch' statements.
In the end, all your AsyncTask instances/objects, regardless which subclass they actually are, are run on one 'global' system queue and 'global' pool of threads. They all share it.
For executing a particular AsyncTask instance/object: You can execute an AsyncTask instance/object only once (i.e. call 'execute' on the object only once).
On Tuesday, November 13, 2012 4:44:16 AM UTC-5, Albertus Kruger wrote:
> Hi,
> I am new to Android programming. I would like to know if it is better to > have seperate AsyncTasks for every screen on your application or is it > better to have one AsynTask class that handles all processing from > different screens on my application ?
> I have looked at stackoverflow but its still not clear to me. I'm thinking > one class might be a lot of overhead and making seperate AsyncTasks for > every screen of my application will be good for performance.
I see several people have already replied, and correctly explained that having just one AsyncTask is a bad idea. But none seem to have commented: the very question, posed as a choice between one AsyncTask vs. one for each screen, seems to imply a fundamental misunderstanding of the purpose of AyncTask.
The purpose of AyncTask is not to handle something for every screen. It is to handle long tasks that do not belong in the UI thread in the first place, the prototypical example being the upload or download of a single file via HTTP, with occasional, light communication back to the UI thread, e.g. progress updates on that up/download.
AsyncTask is not a general purpose multi-tasking tool. As has already been pointed out, you can call the executor only once, and then not again. So it is not good for a background task that sits and waits for messages, processing each in turn. Looper/Handler is more suitable for that. AsyncTask is more useful for "one shot" tasks, and specifically for those that take too long to allow in the UI thread.
On Tuesday, November 13, 2012 1:44:16 AM UTC-8, Albertus Kruger wrote:
> Hi,
> I am new to Android programming. I would like to know if it is better to > have seperate AsyncTasks for every screen on your application or is it > better to have one AsynTask class that handles all processing from > different screens on my application ?
> I have looked at stackoverflow but its still not clear to me. I'm thinking > one class might be a lot of overhead and making seperate AsyncTasks for > every screen of my application will be good for performance.