30

Look at LoaderCustomSupport (Use AsyncTaskLoader) and FragmentRetainInstanceSupport (Use Thread, almost equivalent to AsyncTask)

Both examples have the following similarities.

  • Both do not block UI thread when loading data
  • The data fetching thread is not destroyed when user performs configuration change like screen rotation.
  • When data fetching thread finished fetching data, it can update to the correct Fragment UI

However, there are differences.

AsyncTaskLoader

  • Seems like there is no easy way to update intermediate progress to a progress bar dialog

AsyncTask

  • Not sure on this. But Android documentation seems to recommend AsyncTaskLoader for async data loading and updating final result to UI?

Is there any guideline, or checklist to look at, to make a decision on whether to choose AsyncTaskLoader or AsyncTask, to do a time-consuming loading task and update the result to Fragment's UI?

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875

2 Answers2

26

your question made me interested and tried sometimes to look into the differences. Here i am writing my observations.

  1. For the premature termination the asynchronous task using AsyncTask will continue running in its thread. The processing of the results can soon lead to unrequested results while AsyncTaskLoader handle the premature termination of the activity

  2. AsyncTaskLoader handles activity configuration changes (IE when the user rotates the screen).

  3. AsyncTaskLoader is intended load data for DataAdapters so for this purpose it is best to use AsyncTaskLoader But if you need to change UI (specially fragments) after task completion it is better to use AsyncTask as you can't change fragments in onLoadFinished of AsynTaskLoader.

So to me the usage depends on your task. and if the above 3 points doesnt bother you then the performance is same ( haven't found any documents though , but in this case asynctaskloader is recommended :S)

some related links

AsyncTaskLoader vs AsyncTask

http://andreas-kluck.blogspot.com/2012/02/asynctask-and-asynctaskloader.html

Community
  • 1
  • 1
stinepike
  • 54,068
  • 14
  • 92
  • 112
0

AsyncTaskLoaders, like all Loaders, are intended to solve the rotation problem, which is when an AsyncTask is created on an Activity, then the device is rotated before the task has completed that AsyncTask will be lost with the destruction of the Activity.

It's true that all Loaders currently don't support publishing progress, so if this is a requirement for your situation then you should consider an alternative. If rotation, or any other event that causes the Activity to be destroyed, isn't an issue then just use an AsyncTask, otherwise you might want to use a Service, and register a binder to push progress messages back and forth.

Service and Binder messages are kind of a pain though, in my opinion. I find a much easier solution is to use LocalBroadcastManager to send progress broadcasts from an IntentService (or AsyncTaskLoader) and have a BroadcastReceiver in the Activity receive the broadcast and display the progress.

Peter O.
  • 32,158
  • 14
  • 82
  • 96
cephus
  • 2,027
  • 1
  • 13
  • 10
  • 1
    Note on the `FragmentRetainInstanceSupport` link I had given in the question. With proper help from non-UI fragment, you may handle rotation too in AsyncTask as well. – Cheok Yan Cheng Mar 19 '13 at 01:59
  • So you could put an asynctask in a non-ui fragment and post progress to UI fragments via the activity - if no activity is attached then no progress will be reported? – Sam Jun 19 '14 at 15:25
  • So, if an AsyncTask is running and i rotate the screen, then the AsyncTask will be lost? And, in the same case, AsyncTaskLoader will not be lost? – Kaveesh Kanwal Apr 01 '15 at 10:12
  • 1
    Generally speaking yes. Your Activity will have lost it's reference to the AsyncTaskLoader, but it can be retrieved using the LoaderManager (in a similar manner to the way it was created). There's substantial documentation for implementing Loaders correctly on developer.android.com. I should also add that my original answer here is over 2 years old. Times, and my preferences, have changed since then, and now-a-days I generally just use RxJava (and a singleton) to manage persisting long running operations through a configuration change such as rotation. – cephus Apr 07 '15 at 23:52