参考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之一:基本原理相关推荐

  1. webpack4.0各个击破(6)—— Loader篇

    [摘要] webpack作为前端最火的构建工具,是前端自动化工具链最重要的部分,使用门槛较高.本系列是笔者自己的学习记录,比较基础,希望通过问题 + 解决方式的模式,以前端构建中遇到的具体需求为出发点 ...

  2. Webpack4.0各个击破(6)loader篇

    Webpack4.0各个击破(6)loader篇 一. loader综述 二. 如何写一个loader 三. loader的编译器本质 [参考] 一. loader综述 loader是webpack的 ...

  3. XCP概念和基本原理介绍

    XCP概念和基本原理介绍 ASAM接口模型描述了Slave和Master之间发送和接收命令和数据.为了独立于特定的物理传输层,XCP被细分为协议层和传输层. 根据传输层的不同,可分为XCP ON CA ...

  4. webpack HappyPack多个进程处理loader

    文章目录 文章参考 什么是HappyPack? 作用是什么? HappyPack的基本原理? 使用案例 安装依赖库 webpack.config.js 配置如何使用 happypack 配置说明 we ...

  5. Computer OS系统基本原理

    Computer OS系统基本原理 第一章 绪论(考概念) 什么是OS? o 操作系统是一组控制和管理计算机软硬件资源.合理地对各类作业进行调度以及方便用户使用的程序集合. o 操作系统是位于硬件层( ...

  6. XGBoost4J-Spark基本原理

    XGBoost4J-Spark基本原理 XGBoost4J-Spark是一个项目,旨在通过使XGBoost适应Apache Spark的MLLIB框架,无缝集成XGBoost和Apache Spark ...

  7. Docker基本原理概述

    Docker基本原理概述 Docker是一个用于开发,交付和运行应用程序的开放平台.Docker能够将应用程序与基础架构分开,从而可以快速交付软件.借助Docker,可以以与管理应用程序相同的方式来管 ...

  8. 多机多卡训练基本原理

    多机多卡训练基本原理 在工业实践中,许多较复杂的任务需要使用更强大的模型.强大模型加上海量的训练数据,经常导致模型训练耗时严重.比如在计算机视觉分类任务中,训练一个在ImageNet数据集上精度表现良 ...

  9. MindSpore基本原理

    MindSpore基本原理 • MindSpore介绍 o 自动微分 o 自动并行 • 安装 o pip方式安装 o 源码编译方式安装 o Docker镜像 • 快速入门 • 文档 MindSpore ...

最新文章

  1. 自动驾驶制图中的深度学习
  2. [文档].Altera - 可选择的Nios II的Boot方法
  3. PolarDB数据库性能大赛:95后徐妈的经验分享
  4. netstat命令总结
  5. python asyncio future_Python asyncio.isfuture方法代码示例
  6. 畅通工程(HDU-1232)
  7. linux内核那些事之mmap_region流程梳理
  8. 微软Edge/IE11浏览器将禁用SHA-1证书网站
  9. VBA中 各种数据类型的使用(自定义数据类型Type,数组,数据字典)、读写文件
  10. 8 种 NoSQL 数据库系统对比
  11. 【人工智能】Astar算法求解8数码问题(QDU)
  12. 嵌入式-ADS和Proteus安装
  13. html 时间控件滚动选择器,TimePicker
  14. SpringBoot+zxing批量生成二维码_南国
  15. 实践项目一 项目开发团队分配管理
  16. ios 通过代码调整焦距
  17. 超大玉螺旋丸 -A的个数 蓝桥杯练习题
  18. Java架构师成长直通车:LVS+Nginx实现高可用集群
  19. 数据挖掘冰山立方体构建算法:BUC及实现
  20. [转]Kademlia详解

热门文章

  1. 【传智播客】Javaweb程序设计任务教程 黑马程序员 第四章 课后答案
  2. 最优解法——7-3 将数组中的数逆序存放 (20 分)——10行代码AC
  3. Web前端开发笔记——第二章 HTML语言 第十节 画布标签、音视频标签
  4. 电脑组装与维护教程_男,78年,懂电脑组装、运营维护青岛地区寻找电脑维护合适岗位...
  5. HTML 取消超链接下划线
  6. java代码优化_java代码之美(11)---java代码的优化
  7. 计算机控制的工频机是什么,UPS 如何分类,工频机和高频机区别是什么?
  8. c语言二级指针有什么作用,C语言中二级指针的实例详解
  9. 2020知到python语言应用答案_2020年知到APPPython语言应用第四单元章节测试答案
  10. 交换机登入linux ftp,巧用FTP 实现交换机间配置文件复制