在使用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. react: nextJs koa project basic structure

    1.init nextJs project npm init npm install react react-dom next config script in package.json " ...

  2. 详细分析Redis的持久化操作——RDB与AOF

    一.前言   由于疫情的原因,学校还没有开学,这也就让我有了很多的时间.趁着时间比较多,我终于可以开始学习那些之前一直想学的技术了.最近这几天开始学习Redis,买了本<Redis实战>, ...

  3. MySQL 查询语句优化思路

    query 语句的优化思路和原则主要提现在以下几个方面:1. 优化更需要优化的Query:2. 定位优化对象的性能瓶颈:3. 明确的优化目标:4. 从 Explain 入手:5. 多使用profile ...

  4. kubeadm 默认镜像配置问题引申

    背景: 每次使用功能kubeadm的时候都需要提前准备好镜像,为什么自定义使用的镜像源呢? 在没有翻越围墙时 kubeadm init --kubernetes-version=v1.13.0 --p ...

  5. ReportEventA 错误 ERROR_CRC

    如果 level 的宏弄错了,就会出现这种奇怪的现象.

  6. c语言----- 冒泡排序 for while do-while 递归练习

    1. 冒泡排序简介(默认从小到大排序) 核心思想:只比较相邻的两个元素,如果满足条件就交换    5 8 2 1 6 9 4 3 7 0 目标:0 1 2 3 4 5 6 7 8 9 第一次排序: 5 ...

  7. Ubuntu登陆时忘记密码怎么办

    有时候由于各种原因,用户会忘记自己登陆Ubuntu的登陆密码,这个时候我们能怎么办呢? 第一步:先重启电脑,开机时长按shift键,进入grub菜单: 第二步:按“e”键编辑启动项,显示如下图,将下图 ...

  8. Vue 结合 echarts 原生 html5 实现拖拽排版报表系统

    前言 不知道各位 coder 有没有碰到过许多重复的业务需求,比如排版相类似的报表,只不过是顺序稍微换了一下,就是一个新的页面,虽然基于模板思想来写的话也能减少不少代码,但是相对的不那么方便,笔者最近 ...

  9. L3.二.return

    # 函数的返回值 def get_max(a,b,c): max_num=a if b > max_num: max_num = b if c > max_num: max_num = c ...

  10. 可运行的Java RMI示例和踩坑总结

    简述 资料参考: https://docs.oracle.com/javase/tutorial/rmi/overview.html https://blog.csdn.net/bigtree_372 ...