参考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.

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

Loader之一:基本原理 分类: H1_ANDROID 2013-11-16 10:29 1923人阅读 评论(0) 收藏的更多相关文章

  1. C#多线程(下) 分类: C# 线程 2015-03-09 10:41 153人阅读 评论(0) 收藏

    四.多线程的自动管理(线程池) 在多线程的程序中,经常会出现两种情况: 一种情况: 应用程序中,线程把大部分的时间花费在等待状态,等待某个事件发生,然后才能给予响应 这一般使用ThreadPool(线 ...

  2. 博弈论入门小结 分类: ACM TYPE 2014-08-31 10:15 73人阅读 评论(0) 收藏

    文章原地址:http://blog.csdn.net/zhangxiang0125/article/details/6174639 博弈论:是二人或多人在平等的对局中各自利用对方的策略变换自己的对抗策 ...

  3. iOS开发:创建真机调试证书 分类: ios相关 2015-04-10 10:22 149人阅读 评论(0) 收藏

    关于苹果iOS开发,笔者也是从小白过来的,经历过各种困难和坑,其中就有关于开发证书,生产证书,in_house证书,add_Hoc证书申请过程中的问题,以及上架发布问题.今天就着重说一下关于针对于苹果 ...

  4. 全方位分析Objcetive-C Runtime 分类: ios技术 2015-03-11 22:29 77人阅读 评论(0) 收藏

    本文详细整理了 Cocoa 的 Runtime 系统的知识,它使得 Objective-C 如虎添翼,具备了灵活的动态特性,使这门古老的语言焕发生机.主要内容如下: 引言 简介 与Runtime交互 ...

  5. C#中的线程(上)-入门 分类: C# 线程 2015-03-09 10:56 53人阅读 评论(0) 收藏

    1.     概述与概念 C#支持通过多线程并行地执行代码,一个线程有它独立的执行路径,能够与其它的线程同时地运行.一个C#程序开始于一个单线程,这个单线程是被CLR和操作系统(也称为"主线 ...

  6. 基于命令行编译打包phonegap for android应用 分类: Android Phonegap 2015-05-10 10:33 73人阅读 评论(0) 收藏

    也许你习惯了使用Eclipse编译和打包Android应用.不过,对于使用html5+js开发的phonegap应用,本文建议你抛弃Eclipse,改为使用命令行模式,绝对的快速和方便. 一直以来,E ...

  7. 网站通用登录模块代码 分类: ASP.NET 2014-12-06 10:49 615人阅读 评论(0) 收藏

    1.HTML部分:     <form id="form1" runat="server">     <script src=".. ...

  8. 欧拉回路-Door Man 分类: 图论 POJ 2015-08-06 10:07 4人阅读 评论(0) 收藏

    Door Man Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2476 Accepted: 1001 Description ...

  9. 多校3- RGCDQ 分类: 比赛 HDU 2015-07-31 10:50 2人阅读 评论(0) 收藏

    RGCDQ Time Limit:3000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Practic ...

随机推荐

  1. c++操作当前窗体句柄

    句柄 这一概念是MFC里的一个类.MFC有专门的函数来获取窗体句柄. HWND hwnd; hwnd=CreateWindow("my own windowshandle",&qu ...

  2. Linux下搭建JSP环境

    Linux下搭建JSP环境     作为一名Java EE系统架构工程师,经常需要搭配和建立JSP(Java Server Pages)的开发环境和运行环境,所以本人在平时的工作中积累了一些在Linu ...

  3. golang webservice[ json Martini webframe]

    golang webservice[ json Martini webframe] https://github.com/brunoga/go-webservice-sample 自己修改了一下例子, ...

  4. JAVA 多线程和进程概念的引入

    1.并发和并行 并行:指两个或多个时间在同一时刻发生(同时发生): 并发:指两个或多个事件在一个时间段内发生. 在操作系统中,安装了多个程序,并发指的是在一段时间内宏观上有多个程序同时运行,这在单 C ...

  5. log4j.properties配置与加载应用

    log4j.properties总结:   一.介绍 Log4j是Apache的一个开放源代码项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台.文件.GUI组件.甚至是套接口服务 器 ...

  6. iOS多语言(国际化)开发(尾随系统 + APP内手动设置)

    一:尾随系统切换语言 1>创建好项目project后, 新建一个多语言文件: 2>加入要设置的语言类型: 3>加入成功 细心的朋友可能会发如今English后面写的是3 Files ...

  7. 文档翻译第003篇:Process Monitor帮助文档(Part 3,附Process Monitor的简单演示)

    [导入与导出配置] 一旦您配置了一个筛选器,您能够使用"工具(Tools)"菜单中的"保存筛选器(SaveFilters)"菜单项将其保存.Process Mo ...

  8. Flask项目之手机端租房网站的实战开发(六)

    说明:该篇博客是博主一字一码编写的,实属不易,请尊重原创,谢谢大家! 接着上一篇博客继续往下写 :https://blog.csdn.net/qq_41782425/article/details/8 ...

  9. 下次自己主动登录(记住password)功能

    1:进入cookie插件 <script src="jquery.cookie.js" type="text/javascript"></sc ...

  10. Oracle动态SQL语句

    动态SQL返回游标: create or replace procedure proc_ValidityDueQuery( p_regioncode in number, p_pscode in nu ...