When the Android loader is used, the screen is unlocked and then loaded repeatedly

Source: Internet
Author: User

When using Asynctaskloader, when the phone is unlocked, the data is loaded repeatedly, with the following code:

Static class Couponshopqueryloader extendsasynctaskloader<list<couponstore>> {private int couponid; Public Couponshopqueryloader (context context, int Couponid) {super (context); This.couponid = Couponid;} @Overrideprotected void Onstartloading () {forceload ();} @Overridepublic list<couponstore> Loadinbackground () {//Query data loading}}

At this time, very strange phenomenon is out, each phone after unlocking, the data will be repeated, repeated loading. After consulting Cursorloader source found that the original is too tender, loader use, not strictly abide by the Android official Help document demo use. After modification:

Static class CouponShopQueryLoader2 extendsasynctaskloader<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 */@Overridepublic list<couponstore> Loadinba Ckground () {mdata = Ds.queryshopbycoupon (Couponid, PageNo, page_size); return mdata;} /* Runs on the UI thread */@Overridepublic 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'll be called on the UI thread. If A * Previous load has been completed and was still valid the result may be * passed to the callbacks immediately. * * must is called from the UI thread */@Overrideprotected void onstartloading () {if (mdata! = null) {Deliverresult (Mdata) ;} if (Takecontentchanged () | | Mdata = = null) {forceload ();}} /** * must is called from the UI thread */@Overrideprotected void onstoploading () {LOG.D ("SSS", "onstoploading");//Attemp T to cancel the current load task if Possible.cancelload ();} @Overridepublic void oncanceled (list<couponstore> cursor) {LOG.D ("SSS", "oncanceled");} @Overrideprotected void OnReset () {super.onreset (); LOG.D ("SSS", "onreset");//ensure the loader is stoppedonstoploading (); mdata = null;}}
After the modification, the phenomenon of repeated loading solved, the reason is not rewritten

/** * must is called from the UI thread */@Overrideprotected void onstoploading () {LOG.D ("SSS", "onstoploading");//Attemp T to cancel the current load task if Possible.cancelload ();}

When the phone screen is off, the onstoploading () method is called and the loader should be canceled, and when the screen is unlocked, the onstartloading () method is executed, and the Onstartloading method is judged based on whether the data needs to be reloaded. And if not onstartloading to loader state judgment, it leads to the problem of data re-loading! OK---solved!


Tip: In the study of Android Development, the official documents are actually very good, abide by their written specifications, can make themselves less go a lot of detours.




When the Android loader is used, the screen is unlocked and then loaded repeatedly

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.