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 ...
随机推荐
- git flow配置问题
Mac使用 git flow 的时候,当 -m 进行换行有时消息会被截断,怎么解决? 原因: brew 上的 gnu-getopt 这个包未配置好 步骤: 1. 在 ~/.gitflow_export ...
- Spark SQL源码解析(三)Analysis阶段分析
Spark SQL原理解析前言: Spark SQL源码剖析(一)SQL解析框架Catalyst流程概述 Spark SQL源码解析(二)Antlr4解析Sql并生成树 Analysis阶段概述 首先 ...
- Python模块---制作新冠疫情世界地图()
目录 pyecharts模块 简介 安装pyecharts 测试pyecharts模块 pyecharts实战:绘制新冠肺炎疫情地图 需求分析 请求数据 提取数据 处理数据 制作可视化地图 设置可视化 ...
- PHP入门-1
基本数据类型: 1.整形 2.浮点型 3.字符串 4.布尔型 5.数组和对象 6.null 7.资源类型 8.伪类型 由于php是弱语言,所以他的数据类型不用自己来定义.定义一个数据类型,$name ...
- htaccess 一般配置
一.Apache服务器 <IfModule mod_rewrite.c> Options +FollowSymlinks -Multiviews RewriteEngine on Rewr ...
- css3 文本控制自动换行
text-overflow:ellipsis; white-space:nowrap; overflow:hidden;
- QtConcurrent::run 运行类的成员函数
https://stackoverflow.com/questions/2152355/is-it-possible-to-use-qtconcurrentrun-with-a-function-me ...
- certutil 导入 CA 证书
2019独角兽企业重金招聘Python工程师标准>>> 在linux下使用GoAgent客户端的时候,需要导入CA.cer证书. 安装证书管理工具 apt-get install l ...
- 全网最清晰的ConstraintLayout教程
ConstraintLayout是AndroidStudio2.2新增的一个功能,那么这个到底是什么呢?首先第一点我们知道传统的安卓开发,页面基本都是XML编写实现,特别在一些复杂的页面上需要嵌套多层 ...
- Leetcode2 两数相加 Python
给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和 ...