正在使用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监听屏幕解锁和判断屏幕状态

    开发后台服务的时候经常需要对屏幕状态进行判断,如果是想要监听屏幕解锁事件,可以在配置里面注册action为 android.intent.action.USER_PRESENT的广播,则可以监听解锁事 ...

  2. Android Loader使用时,屏幕解锁后,重复加载

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

  3. 计算Android屏幕解锁组合数

    晚饭时和同事聊到安卓屏幕解锁时会有多少种解锁方案,觉得很有趣,吃完饭开始想办法解题,花了大概2个小时解决.思路如下: 使用索引值0-9表示从左到右.从上到下的9个点,行.列号很容易从索引值得到: 使用 ...

  4. Android 修改屏幕解锁方式

    Android 修改屏幕解锁方式 问题 在手机第一次开机的时候,运行手机激活的APP 在激活APP允许过程中,当用户按电源键的时候,屏幕黑掉,进入锁屏状态 手机默认的锁屏是滑动解锁 用户这个时候再一次 ...

  5. Android 实现两屏幕互相滑动

    Android 实现两屏幕互相滑动 下文来自: http://blog.csdn.net/song_shi_chao/article/details/7081664 ----------------- ...

  6. Android的Activity屏幕切换动画(一)-左右滑动切换

    (国内知名Android开发论坛eoe开发者社区推荐:http://www.eoeandroid.com/) Android的Activity屏幕切换动画(一)-左右滑动切换 在Android开发过程 ...

  7. Qt for Android 程序禁止屏幕旋转

    有时候我们希望让一个程序的界面始终保持在一个方向,不随手机(平板)方向旋转而变化:在AndroidManifest.xml的每一个需要禁止转向的Activity配置中加入 android:screen ...

  8. android多分辨率多屏幕密度下UI适配方案

    相关概念 分辨率:整个屏幕的像素数目,为了表示方便一般用屏幕的像素宽度(水平像素数目)乘以像素高度表示,形如1280x720,反之分辨率为1280x720的屏幕,像素宽度不一定为1280 屏幕密度:表 ...

  9. 设置Android默认锁定屏幕旋转

    /********************************************************************************** * 设置Android默认锁定屏 ...

随机推荐

  1. ImportError with IronPython in C#

    I was using IronPython to execute python code inside my C# implementation lately, and I encountered ...

  2. mysql回想一下基础知识

    创建数据库 creat table test( #整数通常用于int test_id int, #十进制通常使用decimal test_price decimal, #普通文本通常使用.并使用Def ...

  3. 安装gcc 3.4

    安装   gcc 3.4 f**k,不是为了编译0.11内核.我才懒得鸟3.4的版本号 源代码编译被我实践--"不归路",各种报错,我起码不止是了4个版本号的gcc,各种不兼容.各 ...

  4. 乐在其中设计模式(C#) - 策略模式(Strategy Pattern)

    原文:乐在其中设计模式(C#) - 策略模式(Strategy Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 策略模式(Strategy Pattern) 作者:webabc ...

  5. Amazon SQS简单介绍 上篇

    SQS即Simple Queue Service, 是一个分布式的消息队列服务,使用它很easy,消息队列服务能够用来buffer burst, 使整个服务异步处理,不要求组件始终可用. 开发者最初使 ...

  6. 交换A与B值的四种方法

    在网上看到了这样一道面试题,"int A=5,int B=2,怎样交换A与B的值",或许这是一道简单到不能再简单的题,但能作为一道面试题,肯定有其独特之处 大多数人会通过定义第三个 ...

  7. HDU 5052 LCT

    Yaoge's maximum profit Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/ ...

  8. 惠普4431s 笔记本配置

    hp-4431s驱动精灵硬件检测报告 版本:2015.3.26.1363(8.1.326.1363)================================================== ...

  9. 数列的前N项之和

    时间限制: 1 Sec  内存限制: 128 MB 提交: 393  解决: 309 [提交][状态][讨论版] 题目描述 有一分数序列: 2/1 3/2 5/3 8/5 13/8 21/13.... ...

  10. MongoDB CRUD 基础知识

    建立一个良好的发展环境 环境win8 x64,下载并安装省略.经mongodb 的bin文件夹增加windows的path中,为以后使用方便. c盘新建存储目录:c:/data/db 执行服务:WIN ...