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

前端 未结 2 1334
闹比i
闹比i 2020-12-08 00:48

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

Both examples have the following sim

2条回答
  •  北荒
    北荒 (楼主)
    2020-12-08 01:24

    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

提交回复
热议问题