Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
272 views
in Technique[技术] by (71.8m points)

android - Guideline to choose among AsyncTaskLoader and AsyncTask to be used in Fragment

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...