diff options
| author | Dmitri Plotnikov <dplotnikov@google.com> | 2010-06-16 15:38:07 -0700 |
|---|---|---|
| committer | Dmitri Plotnikov <dplotnikov@google.com> | 2010-06-16 15:38:07 -0700 |
| commit | bef9c7a59dc020c5cdcbd555b5212ae5a10e8045 (patch) | |
| tree | 584103dc584ee9dc1ab5997595ebb913c5b70ed7 /core/java/android/content/AsyncTaskLoader.java | |
| parent | ce718947db0d305a8cf015c29e68907d42e1b6cd (diff) | |
Preventing cursor leaks when a query is interrupted
Re-ran runtest cts-os
Change-Id: I518a2a4f842b01d082078e16643aa377a4575237
Diffstat (limited to 'core/java/android/content/AsyncTaskLoader.java')
| -rw-r--r-- | core/java/android/content/AsyncTaskLoader.java | 29 |
1 files changed, 24 insertions, 5 deletions
diff --git a/core/java/android/content/AsyncTaskLoader.java b/core/java/android/content/AsyncTaskLoader.java index f43921faa673..b19c0724b1e5 100644 --- a/core/java/android/content/AsyncTaskLoader.java +++ b/core/java/android/content/AsyncTaskLoader.java @@ -20,15 +20,19 @@ import android.os.AsyncTask; /** * Abstract Loader that provides an {@link AsyncTask} to do the work. - * + * * @param <D> the data type to be loaded. */ public abstract class AsyncTaskLoader<D> extends Loader<D> { final class LoadTask extends AsyncTask<Void, Void, D> { + + private D result; + /* Runs on a worker thread */ @Override protected D doInBackground(Void... params) { - return AsyncTaskLoader.this.loadInBackground(); + result = AsyncTaskLoader.this.loadInBackground(); + return result; } /* Runs on the UI thread */ @@ -36,6 +40,11 @@ public abstract class AsyncTaskLoader<D> extends Loader<D> { protected void onPostExecute(D data) { AsyncTaskLoader.this.dispatchOnLoadComplete(data); } + + @Override + protected void onCancelled() { + AsyncTaskLoader.this.onCancelled(result); + } } LoadTask mTask; @@ -50,6 +59,7 @@ public abstract class AsyncTaskLoader<D> extends Loader<D> { */ @Override public void forceLoad() { + cancelLoad(); mTask = new LoadTask(); mTask.execute((Void[]) null); } @@ -65,11 +75,20 @@ public abstract class AsyncTaskLoader<D> extends Loader<D> { */ public boolean cancelLoad() { if (mTask != null) { - return mTask.cancel(false); + boolean cancelled = mTask.cancel(false); + mTask = null; + return cancelled; } return false; } - + + /** + * Called if the task was canceled before it was completed. Gives the class a chance + * to properly dispose of the result. + */ + public void onCancelled(D data) { + } + void dispatchOnLoadComplete(D data) { mTask = null; deliverResult(data); @@ -78,7 +97,7 @@ public abstract class AsyncTaskLoader<D> extends Loader<D> { /** * Called on a worker thread to perform the actual load. Implementations should not deliver the * results directly, but should return them from this method, which will eventually end up - * calling deliverResult on the UI thread. If implementations need to process + * calling deliverResult on the UI thread. If implementations need to process * the results on the UI thread they may override deliverResult and do so * there. * |
