Android loader use, screen unlock, repeat load

Source: Internet
Author: User

Asynctaskloader time is being used. When the phone is unlocked, repeat the load data, code, as follows:

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 unlocked, the data will be repeated, repeatedly loaded. After consulting Cursorloader source code found that the original is too tender, loader use. There is no strict adherence to the Android official Help documentation for how the demo is used.

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 change. The phenomenon of repeated loading has been overcome, 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, it runs the Onstartloading () method, which is inferred in the Onstartloading method based on whether the data needs to be loaded again. And assuming that the loader state inference is not onstartloading. Causes the problem that data is loaded repeatedly! OK---conquer!


Tip: In learning Android development. The official documents are actually very good, follow their code of writing, can make oneself less go many detours.




Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

Android loader use, screen unlock, repeat load

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.