Android Loader使用時,屏幕解鎖后,重復加載

jopen 10年前發布 | 19K 次閱讀 Android Android開發 移動開發

在使用AsyncTaskLoader時,當手機解鎖后,會重復加載數據,代碼如下:

    static class CouponShopQueryLoader extends
AsyncTaskLoader<List<CouponStore>> {

    private int couponId;  


    public CouponShopQueryLoader(Context context, int couponId) {  
        super(context);  
        this.couponId = couponId;  

    }  

    @Override  
    protected void onStartLoading() {  

        forceLoad();  
    }  

    @Override  
    public List<CouponStore> loadInBackground() {  
        //查詢數據加載  
    }  
}  </pre><a href="/misc/goto?guid=4959617054796548353" target="_blank" title="派生到我的代碼片" style="text-indent:0;"></a></div>

</div> </div>
這時候,很奇怪的現象就出來了,每次手機解鎖后,數據都會重復了,重復加載。經查閱CursorLoader源碼后發現,原來還是自己太嫩了,loader使用時,沒有嚴格遵守android官方幫助文檔demo的使用方式。經修改后:

    static class CouponShopQueryLoader2 extends
AsyncTaskLoader<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 */  
    @Override  
    public List<CouponStore> loadInBackground() {  
        mData = ds.queryShopByCoupon(couponId, pageNo, PAGE_SIZE);  
        return mData;  
    }  

    /* Runs on the UI thread */  
    @Override  
    public 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 
     */  
    @Override  
    protected void onStartLoading() {  
        if (mData != null) {  
            deliverResult(mData);  
        }  
        if (takeContentChanged() || mData == null) {  
            forceLoad();  
        }  
    }  

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

    @Override  
    public void onCanceled(List<CouponStore> cursor) {  
        Log.d("sss", "onCanceled");  
    }  

    @Override  
    protected void onReset() {  
        super.onReset();  
        Log.d("sss", "onReset");  
        // Ensure the loader is stopped  
        onStopLoading();  
        mData = null;  
    }  

}  </pre><a href="/misc/goto?guid=4959617054796548353" target="_blank" title="派生到我的代碼片" style="text-indent:0;"></a></div>

</div> </div> 修改后,重復加載的現象解決了,究其原因是沒有重寫

</div> </div>

    /**

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

當手機屏幕關閉時,會調用onStopLoading()方法,此時應該將loader取消掉,當屏幕解鎖時,會去執行onStartLoading() 方法,在onStartLoading方法中根據數據是否需要重新加載進行判斷。而如果不在onStartLoading進行loader狀態判斷的話, 就導致了數據重復加載的問題! ok---解決了!

來自:http://blog.csdn.net/cml_blog/article/details/40393909

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!