1

Possible Duplicate:
Handler vs AsyncTask vs Thread

Am a newbie to android and am on the way developing my first app...

Which kind of threading is better to create process separate from UI thread?

AsyncTask or extending Thread class with Handler and Message?

I have gone throug this thread.. Put an object in Handler message

The person have told that, "Oh, and I'm moving away from using AsyncTask objects, as I believe they increase code coupling too much.".

What's meant by code coupling? Should I use Java thread with Handler and Message classes or should I use Async task?

Community
  • 1
  • 1
Sathesh
  • 6,323
  • 6
  • 36
  • 48

3 Answers3

1

As this is going to be your first app it would be best to put this question away for a while. Instead you would want to make your application working anyhow. Doing that you will gain enough experience to decide for yourself.

Having said this AsyncTask seems to be easier to use if what you need is to do something in background and present progress and results in UI.

One real problem with either approach is that when you rotate your device your UI gets recreated. And if you store ane references to the older UI in your thread/asynctask and use them your app the crashes.

Alexander Kulyakhtin
  • 47,782
  • 38
  • 107
  • 158
1

AsyncTask uses Java's native threads in the background. The advantage of using them is that you get 3 methods - onPreExecute, doInBackground & onPostExecute which make you life easier.

Mus
  • 1,860
  • 2
  • 16
  • 19
1

AsyncTask is a nice abstraction for well-defined tasks that need to be done on a worker thread (to avoid blocking the main thread), while reporting progress and publishing results to the UI. Examples of such tasks are : downloading file from internet, calling a webservice, querying the database etc. It maintains a pool a worker threads to run these tasks. Using AsyncTask frees you from having to write code to create and mange threads and to dispatch UI updates to the UI thread.

AsyncTask is not appropriate when the background task is an ongoing process rather than a well-defined task. Examples : playing music, continuously tracking location, continuously downloading news feeds etc. In such cases, it is appropriate to create and manage a separate thread.

Sameer
  • 4,379
  • 1
  • 23
  • 23