Android Loader使用时,屏幕解锁后,重复加载
在使用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使用时,屏幕解锁后,重复加载的更多相关文章
- Android Loader使用,屏幕解锁,重复荷载
正在使用AsyncTaskLoader时间.当手机被解锁,重复加载数据,码,如以下: static class CouponShopQueryLoader extends AsyncTaskLoade ...
- Android Studio使用时源码到处报红色警告,运行时又没错
转载地址:http://www.07net01.com/program/2016/04/1452749.html [摘要:正在AS上开辟时,碰到那个题目,翻开全部的Java源文件,右边一起标赤色,找没 ...
- Android PopupWindow使用时注意
项目中使用PopupWindown出现的坑 1.部分设备,PopWindow在Android4.0后版本,出现NullPointerException调用以下方法可解决, fixPopupWindow ...
- Android stdio使用时遇到的一些问题
(1)android stdio加载布局时 Exception raised during rendering: com/android/util/PropertiesMap ...
- Eclipse和Android Studio中的DDMS使用时什么不同?
http://www.jb51.net/softjc/454131.html Eclipse和Android Studio中的DDMS使用时什么不同? 相信很多经常开发Android应用的朋友应该都接 ...
- Android插件化(三):OpenAtlas的插件重建以及使用时安装
Android插件化(三):OpenAtlas的插件重建以及使用时安装 转 https://www.300168.com/yidong/show-2778.html 核心提示:在上一篇博客 An ...
- ym——Android怎样支持多种屏幕
转载请注明本文出自Cym的博客(http://blog.csdn.net/cym492224103),谢谢支持! 原文链接:http://developer.android.com/guide/pra ...
- Android应用如何支持屏幕多尺寸多分辨率问题
作为Android应用程序开发者都知道android是一个“碎片化”的世界.多种系统版本.多种尺寸.多种分辨率.多种机型,还有不同的厂商定制的不同ROM,你开发的应用会在不可预期的手机上报错.这给开发 ...
- Android 中Webview 自适应屏幕
随笔 - 478 文章 - 3 评论 - 113 Android 中Webview 自适应屏幕 webview中右下角的缩放按钮能不能去掉 settings.setDisplayZoomCon ...
随机推荐
- SpringCloud-Gateway 网关路由、断言、过滤
Gateway 简介 是什么? Spring Cloud 全家桶中有个很重要的组件:网关.在 1.x 版本中使用的是 Zuul 网关,但是到了 2.x,由于Zuul的升级不断跳票,Spring Clo ...
- pytorch实现MLP并在MNIST数据集上验证
写在前面 由于MLP的实现框架已经非常完善,网上搜到的代码大都大同小异,而且MLP的实现是deeplearning学习过程中较为基础的一个实验.因此完全可以找一份源码以参考,重点在于照着源码手敲一遍, ...
- Python操作三大主流数据库
Python 标准数据库接口为 Python DB-API,Python DB-API为开发人员提供了数据库应用编程接口. Python 数据库接口支持非常多的数据库,你可以选择适合你项目的数据库: ...
- xpath爬虫实战-爬取小说斗罗大陆第四部
爬取思路 用到的第三方库文件 lxml,requests,fake_agent 用fake_agent里的UserAgent修饰爬虫 用requests进行基本的请求 用lxml进行html的分析 用 ...
- 前端开发--ajax
使用ajax,他是有两个模块的,一个是客户端,一个是服务端. 客户端负责发送数据,发送数据的方式有两种,一种是GET,另一种是POST. 服务端是用来接收,处理数据和发送请求的数据. 要想使用ajax ...
- CRS-0184 Cannot communicate with the CRS daemon
事件背景 rman清理脚本异常.导致磁盘空间爆满(一个环境变量没有设置正确) 释放磁盘空间,进行rman清理 之后,领导把实例重启,但是ams实例没有关闭 环境 系统 : AIX 数据库: Oracl ...
- mysql面试(1)
一一个 SQL 执行行行的很慢,我们要分两种情况讨论:1.大大多数情况下很正常,偶尔很慢,则有如下原因(1).数据库在刷新脏⻚页,例例如 redo log 写满了了需要同步到磁盘.(2).执行行行的时 ...
- 数据包的抓取[tcpdump]的应用
[root@server ~]# yum install tcpdump [root@server ~]# yum install wireshark 1.默认情况下,直接启动tcpdump将监视第一 ...
- build.gradle 详解(一)
简述: 1) Java 开发中有两个大名鼎鼎的项目构建 ANT.Maven. 2) Google 推荐使用的 Android studio 是采用 Gradle 来构建项目.Gradle 是一个非常先 ...
- js 函数对象的继承 inherit 带 插件完整解析版[helpers.js]
前言: 本人纯小白一个,有很多地方理解的没有各位大牛那么透彻,如有错误,请各位大牛指出斧正!小弟感激不尽. 本篇文章为您分析一下原生JS的对象继承方法 需求分析: 1. ...