参考APIDEMO及http://developer.android.com/guide/components/loaders.html#app

1、Introduced in Android 3.0, loaders make it easy to
asynchronously load data in an activity or fragment. Loaders have these characteristics:

They are available to every Activity and Fragment.

They provide asynchronous loading of data.

They monitor the source of their data and deliver new results when the content changes.

They automatically reconnect to the last loader's cursor when being recreated after a configuration change. Thus, they don't need to re-query their data.

2、重要的类及接口

LoaderManager An abstract class associated with an Activity or Fragment for
managing one or more Loader instances. This
helps an application manage longer-running operations in conjunction with the Activity orFragment lifecycle;
the most common use of this is with a CursorLoader,
however applications are free to write their own loaders for loading other types of data. 



There is only one LoaderManager per
activity or fragment. But aLoaderManager can
have multiple loaders.
LoaderManager.LoaderCallbacks A callback interface for a client to interact with the LoaderManager.
For example, you use the onCreateLoader() callback
method to create a new loader.
Loader An abstract class that performs asynchronous loading of data. This is the base class for a loader. You would typically use CursorLoader,
but you can implement your own subclass. While loaders are active they should monitor the source of their data and deliver new results when the contents change.
AsyncTaskLoader Abstract loader that provides an AsyncTask to
do the work.
CursorLoader A subclass of AsyncTaskLoader that
queries the ContentResolver and
returns a Cursor.
This class implements the Loader protocol
in a standard way for querying cursors, building on AsyncTaskLoader to
perform the cursor query on a background thread so that it does not block the application's UI. Using this loader is the best way to asynchronously load data from a ContentProvider,
instead of performing a managed query through the fragment or activity's APIs.

3、一般在以下情况使用Loader:

This section describes how to use loaders in an Android application. An application that uses loaders typically includes the following:

An Activity or Fragment.

An instance of the LoaderManager.

A CursorLoader to load data backed by a ContentProvider. Alternatively, you can implement your own subclass of Loader or AsyncTaskLoader to load data from some other source.

An implementation for LoaderManager.LoaderCallbacks. This is where you create new loaders and manage your references to existing loaders.

A way of displaying the loader's data, such as a SimpleCursorAdapter.

A data source, such as a ContentProvider, when using a CursorLoader.

4、LoaderManager有一个内部接口LoaderManager.LoaderCallbacks,这个接口定义了以下方法:

onCreateLoader() — Instantiate and return a new Loader for the given ID.

onLoadFinished() — Called when a previously created loader has finished its load.

onLoaderReset() — Called when a previously created loader is being reset, thus making its data unavailable.

一般而言,用户不需要直接对Loader进行操作,而是使用此接口的方法,进行间接控制。





5、Loader的一般操作

(1)使用LoaderManager.initLoader()初始化一个Loader,它将直接调用onCreateLoader()方法。

You typically initialize a Loader within the activity's onCreate() method, or within the fragment's onActivityCreated() method. You do this as follows:

getLoaderManager().initLoader(0, null, this);

The initLoader() call ensures that a loader is initialized and active. It has two possible outcomes:

  • If the loader specified by the ID already exists, the last created loader is reused.
  • If the loader specified by the ID does not exist, initLoader() triggers the LoaderManager.LoaderCallbacks method onCreateLoader(). This is where you implement the code to instantiate and return a new loader.

(2)初始化完成后,直接调用onLoadFinished()

If at the point of this call the caller is in its started state, and the requested loader already exists and has generated its data, then the system calls onLoadFinished() immediately (during initLoader()), so you must be prepared for this to happen.

版权声明:本文为博主原创文章,未经博主允许不得转载。

最新文章

  1. qt中信号与槽机制
  2. 如何用OCR图文识别软件在文档里复制内容
  3. GLSL 中的光照计算
  4. CF#FF(255)-div1-C【水题,枚举】
  5. 【原】Hadoop伪分布模式的安装
  6. JavaScript中常用函数(入门级)(持续更新)
  7. 解决ASP.NET MVC AllowAnonymous属性无效导致无法匿名访问控制器的问题
  8. hdoj 4552 怪盗基德的挑战书【求前缀在字符串中出现的次数之和】
  9. 全志A10_linux3.0内核编译记录
  10. 【Android Developers Training】 39. 获取文件信息
  11. hdu3081 Marriage Match II
  12. asp.net网页上获取其中表格中的数据(爬数据)
  13. Python校验文件MD5值
  14. asp.net core Api配置swagger
  15. Apache rewrite地址重写
  16. 快速安装nginx
  17. Flink--DateSet的Transformation简单操作
  18. 桐桐的数学游戏(N皇后)
  19. linux 安装nvm,通过nvm安装node
  20. 今天重装系统后,Windows更新提示“windows update当前无法检查更新,因为未运行服务。您可能需要重新启动计算机”

热门文章

  1. python实现获取文件列表中每一个文件keyword
  2. [NOI.AC#33]bst 线段树
  3. 36.Node.js 工具模块--OS模块系统操作
  4. java根据url获取完整域名
  5. css实现一个缺口小三角
  6. zico源代码分析(二) 数据读取和解析部分
  7. Log4j.properties 属性详解以及 LOG4J日志级别详解
  8. Docker---(4)Docker 部署spring web项目
  9. ClickOnce
  10. 【CS Round #44 (Div. 2 only) D】Count Squares