When Android Loader is used, the screen is unlocked and loaded repeatedly.
When AsyncTaskLoader is used, data is repeatedly loaded when the mobile phone is unlocked. The Code is as follows:
Static class CouponShopQueryLoader extendsAsyncTaskLoader
> {Private int couponId; public CouponShopQueryLoader (Context context, int couponId) {super (context); this. couponId = couponId;} @ Overrideprotected void onStartLoading () {forceLoad () ;}@ Overridepublic List
LoadInBackground () {// query data loading }}
At this time, it is strange that the data is repeated and loaded every time the mobile phone is unlocked. After checking the source code of CursorLoader, I found that I was still too tender. When I used the loader, I did not strictly abide by the demo of the android official help documentation. After modification:
static class CouponShopQueryLoader2 extendsAsyncTaskLoader
> {private List
mData;private int couponId;public CouponShopQueryLoader2(Context context, int couponId) {super(context);this.couponId = couponId;}// final ForceLoadContentObserver mObserver;/* Runs on a worker thread */@Overridepublic List
loadInBackground() {mData = ds.queryShopByCoupon(couponId, pageNo, PAGE_SIZE);return mData;}/* Runs on the UI thread */@Overridepublic void deliverResult(List
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 */@Overrideprotected void onStartLoading() {if (mData != null) {deliverResult(mData);}if (takeContentChanged() || mData == null) {forceLoad();}}/** * Must be called from the UI thread */@Overrideprotected void onStopLoading() {Log.d(sss, onStopLoading);// Attempt to cancel the current load task if possible.cancelLoad();}@Overridepublic void onCanceled(List
cursor) {Log.d(sss, onCanceled);}@Overrideprotected void onReset() {super.onReset();Log.d(sss, onReset);// Ensure the loader is stoppedonStopLoading();mData = null;}}
After modification, the phenomenon of repeated loading is solved, the reason is that it has not been rewritten.
/** * Must be called from the UI thread */@Overrideprotected void onStopLoading() {Log.d(sss, onStopLoading);// Attempt to cancel the current load task if possible.cancelLoad();}
When the screen of the mobile phone is closed, the onStopLoading () method is called. At this time, the loader should be canceled. When the screen is unlocked, The onStartLoading () method will be executed, in the onStartLoading method, determine whether the data needs to be reloaded. If you do not judge the loader status without onStartLoading, it will lead to the problem of repeated data loading! OK --- solved!