Advertisement
Guest User

Untitled

a guest
Mar 20th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.81 KB | None | 0 0
  1. public abstract class AbstractObjectLoader<R> extends AsyncTaskLoader<R> {
  2.  
  3.     private R result;
  4.  
  5.     public AbstractObjectLoader(Context context) {
  6.         super(context);
  7.     }
  8.  
  9.     @Override
  10.     public void deliverResult(R result) {
  11.         if (isReset()) {
  12.             // An async query came in while the loader is stopped.  We
  13.             // don't need the result.
  14.             if (result != null) {
  15.                 onReleaseResources(result);
  16.             }
  17.         }
  18.         R oldResult = this.result;
  19.         this.result = result;
  20.  
  21.         if (isStarted()) {
  22.             // If the Loader is currently started, we can immediately
  23.             // deliver its results.
  24.             super.deliverResult(result);
  25.         }
  26.  
  27.         // At this point we can release the resources associated with
  28.         // 'oldResult' if needed; now that the new result is delivered we
  29.         // know that it is no longer in use.
  30.         if (oldResult != null) {
  31.             onReleaseResources(oldResult);
  32.         }
  33.     }
  34.  
  35.     /**
  36.      * Handles a request to start the Loader.
  37.      */
  38.     @Override
  39.     protected void onStartLoading() {
  40.         if (this.result != null) {
  41.             // If we currently have a result available, deliver it
  42.             // immediately.
  43.             deliverResult(this.result);
  44.         }
  45.  
  46.         if (takeContentChanged() || this.result == null) {
  47.             // If the data has changed since the last time it was loaded
  48.             // or is not currently available, start a load.
  49.             forceLoad();
  50.         }
  51.     }
  52.  
  53.     /**
  54.      * Handles a request to stop the Loader.
  55.      */
  56.     @Override
  57.     protected void onStopLoading() {
  58.         // Attempt to cancel the current load task if possible.
  59.         cancelLoad();
  60.     }
  61.  
  62.     /**
  63.      * Handles a request to cancel a load.
  64.      */
  65.     @Override
  66.     public void onCanceled(R result) {
  67.         super.onCanceled(result);
  68.  
  69.         // At this point we can release the resources associated with 'result'
  70.         // if needed.
  71.         onReleaseResources(result);
  72.     }
  73.  
  74.     /**
  75.      * Handles a request to completely reset the Loader.
  76.      */
  77.     @Override
  78.     protected void onReset() {
  79.         super.onReset();
  80.  
  81.         // Ensure the loader is stopped
  82.         onStopLoading();
  83.  
  84.         // At this point we can release the resources associated with 'result'
  85.         // if needed.
  86.         if (this.result != null) {
  87.             onReleaseResources(this.result);
  88.             this.result = null;
  89.         }
  90.     }
  91.  
  92.     /**
  93.      * Helper function to take care of releasing resources associated
  94.      * with an actively loaded data set.
  95.      */
  96.     protected void onReleaseResources(R result) {
  97.         // For a simple List<> there is nothing to do.  For something
  98.         // like a Cursor, we would close it here.
  99.     }
  100.  
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement