标签:
正在使用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() { //查询数据载入 } }
经改动后:
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(); }
提示:在学习android开发中。官方文档事实上是非常好的,遵守他们的编写规范,能够使自己少走好多弯路。
版权声明:本文博客原创文章,博客,未经同意,不得转载。
标签:
原文地址:http://www.cnblogs.com/bhlsheji/p/4741366.html