Loader之一:基本原理 分类: H1_ANDROID 2013-11-16 10:29 1923人阅读 评论(0) 收藏
2013-11-16 10:29
411 查看
参考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、重要的类及接口
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:
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、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 Activityor Fragmentfor managing one or more Loaderinstances. This helps an application manage longer-running operations in conjunction with the Activityor Fragmentlifecycle; 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 LoaderManagerper activity or fragment. But a LoaderManagercan 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 AsyncTaskto do the work. |
CursorLoader | A subclass of AsyncTaskLoaderthat queries the ContentResolverand returns a Cursor. This class implements the Loaderprotocol in a standard way for querying cursors, building on AsyncTaskLoaderto 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. |
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之二:CursorLoader基本实例 分类: H1_ANDROID 2013-11-16 10:50 5447人阅读 评论(0) 收藏
- android中的数据库操作ZZ 分类: Android数据存储 2014-05-30 10:58 88人阅读 评论(0) 收藏
- setContentView剖解 分类: Android界面和组件 2014-07-10 16:56 76人阅读 评论(0) 收藏
- android 完整地操作数据库--… 分类: Android开发 2014-05-30 10:58 93人阅读 评论(0) 收藏
- Android开发之WebService介绍ZZ 分类: Android开发 2014-05-30 10:58 77人阅读 评论(0) 收藏
- android listview与sqlite数… 分类: Android数据存储 2014-05-30 10:58 67人阅读 评论(0) 收藏
- 一些Android拨号盘的基本代码 分类: Android 2015-07-10 18:28 10人阅读 评论(0) 收藏
- android RadioGroup和RadioB… 分类: Android开发 2014-05-30 10:58 75人阅读 评论(0) 收藏
- android 判断字符串是否为数字或中文或字母 分类: Android JAVA 2015-06-07 21:07 29人阅读 评论(0) 收藏
- Android数据库Cursor操作:I… 分类: Android数据存储 2014-05-30 10:58 70人阅读 评论(0) 收藏
- android屏幕页面实现滚动,页面跳… 分类: Android开发 2014-05-30 10:58 58人阅读 评论(0) 收藏
- try、catch、finally及finally执行… 分类: Android其他 2014-05-30 10:58 92人阅读 评论(0) 收藏
- Android: R cannot&nbsp… 分类: Android开发 2014-05-30 10:58 80人阅读 评论(0) 收藏
- Android Studio安装及环境配置笔记 分类: Android 2015-07-08 15:00 10人阅读 评论(0) 收藏
- try catch finally 用法 分类: Android其他 2014-05-30 10:58 54人阅读 评论(0) 收藏
- Android中的消息机制:Handler消息传递机制 分类: H1_ANDROID 2013-10-27 22:54 1755人阅读 评论(0) 收藏
- VMWARE Workstation 10 安装Ghost 版本WIN7解决办法 分类: Android其他 2015-05-13 19:34 41人阅读 评论(0) 收藏
- [ZZ]android sqllite数… 分类: Android数据存储 2014-05-30 10:58 61人阅读 评论(0) 收藏
- 处理Android SQLite -&n… 分类: Android数据存储 2014-05-30 10:58 95人阅读 评论(0) 收藏
- Android对话框(Dialog)小结 分类: Android 2015-07-02 23:49 10人阅读 评论(0) 收藏