Loader unable to retain itself during certain configuration change

前端 未结 4 616
渐次进展
渐次进展 2020-12-13 06:59

According to http://developer.android.com/guide/components/loaders.html, one of the nice thing about loader is that, it is able to retain its data during configuration chang

4条回答
  •  孤城傲影
    2020-12-13 07:44

    I've had success subclassing AsyncTaskLoader and making a few tweaks to its methods.

    public class FixedAsyncTaskLoader extends AsyncTaskLoader {
    
        private D result;
    
        public FixedAsyncTaskLoader(Context context) {
            super(context);
        }
    
        @Override
        protected void onStartLoading() {
            if (result != null) {
                deliverResult(result);
            } else {
                forceLoad();
            }
        }
    
        @Override
        public void deliverResult(T data) {
            result = data;
    
            if (isStarted()) {
                super.deliverResult(result);
            }
        }
    
        @Override
        protected void onReset() {
            super.onReset();
            onStopLoading();
    
            result = null;
        }
    
        @Override
        protected void onStopLoading() {
            cancelLoad();
        }
    }
    

提交回复
热议问题