在使用AsyncTaskLoader时,当手机解锁后,会重复加载数据,代码如下:

	static class CouponShopQueryLoader extends
AsyncTaskLoader<List<CouponStore>> { private int couponId; public CouponShopQueryLoader(Context context, int couponId) {
super(context);
this.couponId = couponId; } @Override
protected void onStartLoading() { forceLoad();
} @Override
public List<CouponStore> loadInBackground() {
//查询数据加载
}
}

这时候,很奇怪的现象就出来了,每次手机解锁后,数据都会重复了,重复加载。经查阅CursorLoader源码后发现,原来还是自己太嫩了,loader使用时,没有严格遵守android官方帮助文档demo的使用方式。经修改后:

	static class CouponShopQueryLoader2 extends
AsyncTaskLoader<List<CouponStore>> { private List<CouponStore> mData;
private int couponId; public CouponShopQueryLoader2(Context context, int couponId) {
super(context);
this.couponId = couponId; } // final ForceLoadContentObserver mObserver; /* Runs on a worker thread */
@Override
public List<CouponStore> loadInBackground() {
mData = ds.queryShopByCoupon(couponId, pageNo, PAGE_SIZE);
return mData;
} /* Runs on the UI thread */
@Override
public void deliverResult(List<CouponStore> data) {
if (isReset()) {
return;
} if (isStarted()) {
super.deliverResult(data);
}
} /**
* Starts an asynchronous load of the contacts list data. When the
* result is ready the callbacks will be called on the UI thread. If a
* previous load has been completed and is still valid the result may be
* passed to the callbacks immediately.
*
* Must be called from the UI thread
*/
@Override
protected void onStartLoading() {
if (mData != null) {
deliverResult(mData);
}
if (takeContentChanged() || mData == null) {
forceLoad();
}
} /**
* Must be called from the UI thread
*/
@Override
protected void onStopLoading() {
Log.d("sss", "onStopLoading");
// Attempt to cancel the current load task if possible.
cancelLoad();
} @Override
public void onCanceled(List<CouponStore> cursor) {
Log.d("sss", "onCanceled");
} @Override
protected void onReset() {
super.onReset();
Log.d("sss", "onReset");
// Ensure the loader is stopped
onStopLoading();
mData = null;
} }

修改后,重复加载的现象解决了,究其原因是没有重写

	/**
* Must be called from the UI thread
*/
@Override
protected void onStopLoading() {
Log.d("sss", "onStopLoading");
// Attempt to cancel the current load task if possible.
cancelLoad();
}

当手机屏幕关闭时,会调用onStopLoading()方法,此时应该将loader取消掉,当屏幕解锁时,会去执行onStartLoading()方法,在onStartLoading方法中根据数据是否需要重新加载进行判断。而如果不在onStartLoading进行loader状态判断的话,就导致了数据重复加载的问题! ok---解决了!

提示:在学习android开发中,官方文档其实是很好的,遵守他们的编写规范,可以使自己少走好多弯路。

Android Loader使用时,屏幕解锁后,重复加载的更多相关文章

  1. Android Loader使用,屏幕解锁,重复荷载

    正在使用AsyncTaskLoader时间.当手机被解锁,重复加载数据,码,如以下: static class CouponShopQueryLoader extends AsyncTaskLoade ...

  2. Android Studio使用时源码到处报红色警告,运行时又没错

    转载地址:http://www.07net01.com/program/2016/04/1452749.html [摘要:正在AS上开辟时,碰到那个题目,翻开全部的Java源文件,右边一起标赤色,找没 ...

  3. Android PopupWindow使用时注意

    项目中使用PopupWindown出现的坑 1.部分设备,PopWindow在Android4.0后版本,出现NullPointerException调用以下方法可解决, fixPopupWindow ...

  4. Android stdio使用时遇到的一些问题

    (1)android stdio加载布局时 Exception raised during rendering: com/android/util/PropertiesMap             ...

  5. Eclipse和Android Studio中的DDMS使用时什么不同?

    http://www.jb51.net/softjc/454131.html Eclipse和Android Studio中的DDMS使用时什么不同? 相信很多经常开发Android应用的朋友应该都接 ...

  6. Android插件化(三):OpenAtlas的插件重建以及使用时安装

    Android插件化(三):OpenAtlas的插件重建以及使用时安装 转 https://www.300168.com/yidong/show-2778.html    核心提示:在上一篇博客 An ...

  7. ym——Android怎样支持多种屏幕

    转载请注明本文出自Cym的博客(http://blog.csdn.net/cym492224103),谢谢支持! 原文链接:http://developer.android.com/guide/pra ...

  8. Android应用如何支持屏幕多尺寸多分辨率问题

    作为Android应用程序开发者都知道android是一个“碎片化”的世界.多种系统版本.多种尺寸.多种分辨率.多种机型,还有不同的厂商定制的不同ROM,你开发的应用会在不可预期的手机上报错.这给开发 ...

  9. Android 中Webview 自适应屏幕

    随笔 - 478  文章 - 3  评论 - 113 Android 中Webview 自适应屏幕   webview中右下角的缩放按钮能不能去掉 settings.setDisplayZoomCon ...

随机推荐

  1. 基于MySQL Binlog的Elasticsearch数据同步实践

    一.为什么要做 随着马蜂窝的逐渐发展,我们的业务数据越来越多,单纯使用 MySQL 已经不能满足我们的数据查询需求,例如对于商品.订单等数据的多维度检索. 使用 Elasticsearch 存储业务数 ...

  2. tensorflow1.0 构建神经网络做图片分类

    import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data mnist = input_dat ...

  3. 进阶 Linux基本命令-1

    vmware三种网络模式1,桥接虚拟机直接连接外网,局域网.宿主机电脑不提供路由. 2,NAT网络地址转换,家庭网 3,host only 只能和宿主电脑打交道 Linux命令形式 命令 +[参数]+ ...

  4. 集合-ArrayList 源码解析

    ArrayList是一种以数组实现的List,与数组相比,它具有动态扩展的能力,因此也可称之为动态数组. 类图 ArrayList实现了List, RandomAccess, Cloneable, j ...

  5. Spring Boot JPA 中transaction的使用

    文章目录 @Transactional的实现 @Transactional的使用 Transaction的传播级别 REQUIRED SUPPORTS MANDATORY NEVER NOT_SUPP ...

  6. Linux网络服务第六章PXE高效能批量网络装机

    1.IP地址配置 2.关闭防火墙以及selinux状态如下 systemctl  stop     firewalld Iptables -F Setenforce 0 三.部署FTP服务 1.安装F ...

  7. 【Linux常见命令】mv命令

    mv - move (rename) files mv命令用来为文件或目录改名.或将文件或目录移入其它位置. 语法: mv [OPTION]... [-T] SOURCE DEST mv [OPTIO ...

  8. 命令替换、权限、chmod、特殊权限

    命令替换 把字符串里面的命令先执行再把该字符串输出,与PHP的""里面的变量被执行一样. $(COMMAND) `COMMAND` [root@jiakang ~]# echo & ...

  9. 使nfs同步生效

    我们在使用nfs的时候会遇到往nfs server端写个文件,client需要很久才能生效,我们想让立刻生效有两种办法:1.mount -t nfs -o noac IP:/data/xxxx /xx ...

  10. Linux下进程的创建(system(); fork(); exec*())

    0. system(); system()函数通过调用shell程序来执行所指向的命令(效率低),相当于先fork(),再execve(): 特点:原进程和子进程各自运行,且原进程需要等子进程运行完后 ...