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