Loader之一:基本原理
参考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 formanaging one or more Loader instances. Thishelps 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 peractivity or fragment. But a LoaderManager canhave multiple loaders. |
LoaderManager.LoaderCallbacks |
A callback interface for a client to interact with the LoaderManager .For example, you use the onCreateLoader() callbackmethod 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 todo the work. |
CursorLoader |
A subclass of AsyncTaskLoader thatqueries the ContentResolver andreturns a Cursor .This class implements the Loader protocolin a standard way for querying cursors, building on AsyncTaskLoader toperform 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之一:基本原理的更多相关文章
- webpack4.0各个击破(6)—— Loader篇
webpack作为前端最火的构建工具,是前端自动化工具链最重要的部分,使用门槛较高.本系列是笔者自己的学习记录,比较基础,希望通过问题 + 解决方式的模式,以前端构建中遇到的具体需求为出发点,学习we ...
- webpack4.0各个击破(6)—— Loader篇【华为云技术分享】
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/devcloud/article/detai ...
- webpack的loader的原理和实现
想要实现一个loader,需要首先了解loader的基本原理和用法. 1. 使用 loader是处理模块的解析器. module: { rules: [ { test: /\.css$/, use: ...
- Webpack4.0各个击破(6)loader篇
目录 一. loader综述 二. 如何写一个loader 三. loader的编译器本质 [参考] 一. loader综述 loader是webpack的核心概念之一,它的基本工作流是将一个文件以字 ...
- PHP 实现自动加载
自动载入主要是省去了一个个类去 include 的繁琐,在 new 时动态的去检查并 include 相应的 class 文件. 先上代码: //index.php <?php class Cl ...
- Loader之一:基本原理 分类: H1_ANDROID 2013-11-16 10:29 1923人阅读 评论(0) 收藏
参考APIDEMO及http://developer.android.com/guide/components/loaders.html#app 1.Introduced in Android 3.0 ...
- Trusted Execution Technology (TXT) --- 基本原理篇
版权声明:本文为博主原创文章,未经博主允许不得转载. http://www.cnblogs.com/tsec/p/8409600.html 1. Intel TXT 介绍 TXT是Trusted Ex ...
- Webpack 下使用 web workers 及 基本原理 和 应用场景
_ 阅读目录 一:web workers的基本原理 二:web Workers 的基本用法 三:在webpack中配置 Web Workers 四:Web Worker的应用场景 回到顶部 一:web ...
- 揭秘webpack loader
前言 Loader(加载器) 是 webpack 的核心之一.它用于将不同类型的文件转换为 webpack 可识别的模块.本文将尝试深入探索 webpack 中的 loader,揭秘它的工作原理,以及 ...
随机推荐
- (转)创建和查看Javadoc文档
原地址:http://jinnaxu-tju-edu-cn.iteye.com/blog/667177 Javadoc是Sun公司提供的一个技术,它从程序源代码中抽取类.方法.成员等注释形成一个和源代 ...
- Android方法的传递值及其改变
MainActivity如下: package cn.testchangevar; import android.os.Bundle; import android.view.View; import ...
- haproxy之负载均衡算法
backend blance 算法 动态hash的特点 服务器运行时就可进行权重调整,即只需要重新载入配置文件即可,不需要重新启动haproxy 支持慢速启动,即重新恢复的后端 ...
- kururu的VHDL学习笔记
最近开始做课程设计,VHDL设计一个中央空调的控制程序.所以开始学习VHDL,在这篇文章里面记录一些自己的笔记,期望对于同样的初学者有些借鉴意义~ 编写VHDL所需的工具: 那自然很是quartus啦 ...
- 序列化类型为“System.Reflection.Module”的对象时检测到循环引用
在使用ajax调用web services时,正好返回的类型为datatable,想用通过json方式直接解析,但调用后,得到如下错误: 序列化类型为“System.Reflection.Module ...
- 数据的加密传输——单片机上实现TEA加密解密算法
各位大侠在做数据传输时,有没有考虑过把数据加密起来进行传输,若在串口或者无线中把所要传的数据加密起来,岂不是增加了通信的安全性.常用的加密解密算法比如DES.RSA等,受限于单片机的内存和运算速度,实 ...
- C# ReferenceEquals(), static Equals(), instance Equals(), 和运算行符==之间的关系
C#充许你同时创建值类型和引用类型.两个引用类型的变量在引用同一个对象时,它们是相等的,就像引用到对象的ID一样.两个值类型的变量在它们的类型和内容都是相同时,它们应该是相等的.这就是为什么相等测试要 ...
- jQuery 序列化表单数据 serialize() serializeArray()
1.serialize()方法 格式:var data = $("form").serialize(); 功能:将表单内容序列化成一个字符串. 这样在ajax提交表单数据时,就不用 ...
- Eclipse调试Bug的七种常用技巧(转)
注意事项及小结: (1)Line Breakpoint:如果设置Conditional,监控的变量需要比当前行高一级block,譬如for(int i=0;i<20;i++)中的i,fo ...
- 理解最短路径——迪杰斯特拉(dijkstra)算法
原址地址:http://ibupu.link/?id=29 1. 迪杰斯特拉算法简介 迪杰斯特拉(dijkstra)算法是典型的用来解决最短路径的算法,也是很多教程中的范例,由荷兰计算机科 ...