Android Loader使用,螢幕解鎖,重複荷載

來源:互聯網
上載者:User

標籤:

正在使用AsyncTaskLoader時間。當手機被解鎖,重複載入資料,碼,如以下:

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() {//查詢資料載入}}

這時候,非常奇怪的現象就出來了,每次手機解鎖後,資料都會反覆了,反覆載入。經查閱CursorLoader原始碼後發現,原來還是自己太嫩了,loader使用時。沒有嚴格遵守android官方協助文檔demo的使用方式。

經改動後:

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> loadInBackground() {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 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<CouponStore> cursor) {Log.d("sss", "onCanceled");}@Overrideprotected void onReset() {super.onReset();Log.d("sss", "onReset");// Ensure the loader is stoppedonStopLoading();mData = null;}}
改動後。反覆載入的現象攻克了,究其原因是沒有重寫

/** * Must be called from the UI thread */@Overrideprotected void onStopLoading() {Log.d("sss", "onStopLoading");// Attempt to cancel the current load task if possible.cancelLoad();}

當手機螢幕關閉時,會調用onStopLoading()方法,此時應該將loader取消掉,當螢幕解鎖時,會去運行onStartLoading()方法,在onStartLoading方法中依據資料是否須要又一次載入進行推斷。而假設不在onStartLoading進行loader狀態推斷的話。就導致了資料反覆載入的問題! ok---攻克了!


提示:在學習android開發中。官方文檔事實上是非常好的,遵守他們的編寫規範,能夠使自己少走好多彎路。




著作權聲明:本文部落格原創文章,部落格,未經同意,不得轉載。

Android Loader使用,螢幕解鎖,重複荷載

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.