When Android Loader is used, the screen is unlocked and loaded repeatedly.

Source: Internet
Author: User

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!

 

 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.