AsyncTaskLoader 与 AsyncTask [英] AsyncTaskLoader vs AsyncTask
问题描述
由于Honeycomb
和v4 兼容库
,可以使用AsyncTaskLoader
.据我了解,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.
是否建议使用 AsyncTaskLoader
而不是 AsyncTask
?LoaderManager
是否也出现在图片中?
Is it recommended to use AsyncTaskLoader
instead of AsyncTask
? Does LoaderManager
get in the picture too?
但我还没有找到任何关于如何正确使用 AsyncTaskLoader
的好例子.文档也没有提供示例.谁能提供一些很好的例子.
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.
推荐答案
您可以查看兼容性库的源代码以获取更多信息.FragmentActivity
的作用是:
You can have a look at the compatibility library's source code to get more info. What a FragmentActivity
does is:
- 保留
LoaderManager
的列表 - 通过使用
onRetainNonConfigurationInstance()
保存实例,确保在翻转手机(或发生其他配置更改)时它们不会被破坏 - 在您的活动中调用
initLoader()
时启动正确的加载器
- keep a list of
LoaderManager
's - make sure they don't get destroyed when you flip your phone (or another configuration change occurs) by saving instances using
onRetainNonConfigurationInstance()
- kick the right loader when you call
initLoader()
in your Activity
您需要使用 LoaderManager
与加载器交互,并提供所需的回调来创建加载器并使用它们返回的数据填充视图.
You 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.
通常它应该比自己管理 AsyncTask
更容易.但是,AsyncTaskLoader
的文档并不完整,因此您应该研究文档中的示例和/或在 CursorLoader
之后为您的代码建模.
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 after CursorLoader
.
这篇关于AsyncTaskLoader 与 AsyncTask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!