AsyncTaskLoader 与 AsyncTask
由于Honeycomb
和v4兼容性库
,可以使用AsyncTaskLoader
。据我了解,AsyncTaskLoader
可以通过屏幕翻转等配置更改来生存。
是否建议使用AsyncTaskLoader
而不是AsyncTask
? LoaderManager
也参与其中吗?
但我还没有找到任何关于如何正确使用 AsyncTaskLoader 的好例子。该文档也没有提供示例。谁能提供一些好的例子。
Since Honeycomb
and the v4 Compatibility Library
it is possible to use AsyncTaskLoader
. From what I understand, the AsyncTaskLoader
can survive through config changes like screen flips.
Is it recommended to use AsyncTaskLoader
instead of AsyncTask
? Does LoaderManager
get in the picture too?
But I haven't found any good example(s) about how to correctly use the AsyncTaskLoader
. The docs also provide no examples. Can anyone provide some good examples.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以查看兼容性库的源代码以获取更多信息。
FragmentActivity
的作用是:LoaderManager
列表,onRetainNonConfigurationInstance()
initLoader()
时,启动正确的加载程序您需要使用
LoaderManager
与加载程序交互,并提供需要回调来创建加载程序并使用它们返回的数据填充您的视图。一般来说,它应该比您自己管理 AsyncTask 更容易。但是,
AsyncTaskLoader
没有完整的文档记录,因此您应该研究文档中的示例和/或在CursorLoader
之后对代码进行建模。You can have a look at the compatibility library's source code to get more info. What a
FragmentActivity
does is:LoaderManager
'sonRetainNonConfigurationInstance()
initLoader()
in your ActivityYou need to use the
LoaderManager
to interface with the loaders, and provide the needed callbacks to create your loader(s) and populate your views with the data they return.Generally it should be easier than managing
AsyncTask
's yourself. However,AsyncTaskLoader
is not exactly well documented, so you should study the example in the docs and/or model your code afterCursorLoader
.比较 AsyncTaskLoader 与 AsyncTask,您可能知道,当您旋转设备屏幕时,它可能会破坏并重新创建您的 Activity。为了说清楚。让我们想象一下,在网络事务正在进行时旋转您的设备:
AsyncTask将再次作为后台线程重新执行,而之前的后台线程处理将变得多余和僵化。
AsyncTaskLoader只会根据之前在Loader Manager中注册的Loader ID重新使用,因此可以避免重新执行网络事务。
总之,AsyncTaskLoader 可以防止后台线程的重复,并消除僵尸活动的重复。
Comparing AsyncTaskLoader vs. AsyncTask, as you may know, when you rotate your device screen, it may destroy and re-create your activity. To make it clear. let's imagine rotating your device while networking transaction is going on:
AsyncTask will be re-executed as a background thread again, and the previous background thread processing will just be redundant and zombie.
AsyncTaskLoader will just be re-used basing on Loader ID that was registered in Loader Manager before, so re-executing network transaction will be avoided.
In summary, AsyncTaskLoader prevents duplication of background threads and eliminates duplication of zombie activities.
AsyncTaskLoader 执行与 AsyncTask 相同的功能,但更好一些。它可以更轻松地处理 Activity 配置更改,并且它在 Fragment 和 Activity 的生命周期内运行。好处是 AsyncTaskLoader 可以在任何使用 AsyncTask 的情况下使用。任何时候需要将数据加载到内存中以供 Activity/Fragment 处理时,AsyncTaskLoader 都可以更好地完成这项工作。
不过,使用 AsyncTask 存在一些问题:
AsyncTaskLoader 文档
AsyncTaskLoader performs the same function as the AsyncTask, but a bit better. It can handle Activity configuration changes more easily, and it behaves within the life cycles of Fragments and Activities. The nice thing is that the AsyncTaskLoader can be used in any situation that the AsyncTask is being used. Anytime data needs to be loaded into memory for the Activity/Fragment to handle, The AsyncTaskLoader can do the job better.
There are a few issues with using AsyncTasks, though:
AsyncTaskLoader doc
其他答案中描述之外的一些差异:
当在 AsyncTask 上使用 AsyncTaskLoader 时:
AsyncTaskLoader 使我们可以自由加载旧的缓存数据,直到新的缓存数据数据由
forceLoad()
返回
我们可以通过
setUpdateThrottle()
为AsyncTaskLoader设置延迟,这样可以防止对客户端(Activity/Fragment)的连续更新< /p>AsyncTaskLoader 可以共享给多个片段,前提是它们具有共同的父 Activity 并且从
getActivity().getSupportLoaderManager()
AsyncTaskLoader 会被
LoaderManger
销毁。而如果AsyncTasks的调用者活动被破坏,我们需要手动销毁它。这节省了我们编写所有清理内容的时间。 AsyncTaskLoader 与各自的生命周期配合得很好。所以,AsyncTaskLoader 比 AsyncTask 好得多。
Some differences other than described in other answers:
When using AsyncTaskLoader over AsyncTask:
AsyncTaskLoader gives us liberty to load old cached data until new data is returned by
forceLoad()
We can set delays to AsyncTaskLoader by
setUpdateThrottle()
which can prevent consecutive updates to client (Activity/Fragment)AsyncTaskLoader can be shared to multiple fragments if they have common parent activity and if it was started from
getActivity().getSupportLoaderManager()
AsyncTaskLoader is destroyed by
LoaderManger
when its linked activity is no more available. while we need to manually destroy AsyncTasks if its caller activity destroys. This saves our time from writing all the clearing stuff. AsyncTaskLoader plays well with their respective lifecycles.So, AsyncTaskLoader is way better than AsyncTask.