Android Open Source - Loaders Abstract Loader






From Project

Back to project page Loaders.

License

The source code is released under:

Apache License

If you think the Android project Loaders listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.github.vsouhrada.loaders;
//from   www .j a  v a2 s .c  o m
import android.content.Context;
import android.support.v4.content.AsyncTaskLoader;


/**
 * <p>This class is very useful and powerful in case of that you need to work with your own objects.</p>
 * <p>This class cannot handle with {@link android.database.Cursor} object. If you want to use
 * {@link android.database.Cursor} - please use class {@link AbstractCursorLoader} instead of this
 * class.</p>
 *
 * @param <T>
 * @author vsouhrada
 * @version 1.0.0
 * @see android.support.v4.content.AsyncTaskLoader<T>
 * @since 1.0.0
 */
public abstract class AbstractLoader<T> extends AsyncTaskLoader<T> {

  private T lastData;

  /**
   * In this method you should load data and return it as your object which is used by this class.
   * Method is called by {@link #loadInBackground()} method.
   *
   * @return implementation must return
   * @since 1.0.0
   */
  abstract protected T buildLoader();

  /**
   * Start watching for changes in the app data
   * Method is called by {@link #startLoading()} method.
   *
   * @since 1.0.0
   */
  abstract protected void registerObservers();

  /**
   * Stop monitoring for changes. Method is call by {@link #onReset()} method
   *
   * @since 1.0.0
   */
  abstract protected void unregisterObservers();

  /**
   * Default constructor
   *
   * @param context current context
   * @since 1.0.0
   */
  public AbstractLoader(Context context) {
    super(context);
  }

  /**
   * {@inheritDoc}
   *
   * @since 1.0.0
   */
  @Override
  public T loadInBackground() {
    return buildLoader();
  }

  /**
   * Runs on the UI thread. Delivering the results from the background thread to
   * whatever is using the T.
   *
   * @param data current data
   * @since 1.0.0
   */
  @Override
  public void deliverResult(T data) {
    if (isReset()) {
      // An async query came in while the loader is stopped
      onReleaseResources(data);

      return;
    }

    T oldData = lastData;
    lastData = data;

    if (isStarted()) {
      // If the Loader is currently started, we can immediately
      // deliver its results.
      super.deliverResult(data);
    }

    // At this point we can release the resources associated with
    // 'oldData' if needed; now that the new result is delivered we
    // know that it is no longer in use.
    if (oldData != null) {
      onReleaseResources(oldData);
    }
  }

  /**
   * Take care of loading data. This is not called by clients directly, but as a result of a call to startLoading()
   *
   * @since 1.0.0
   */
  @Override
  protected void onStartLoading() {
    if (lastData != null) {
      // If we currently have a result available, deliver it
      // immediately.
      super.deliverResult(lastData);
    }

    // Start watching for changes in the app data.
    registerObservers();

    if (takeContentChanged() || lastData == null) {
      // If the data has changed since the last time it was loaded
      // or is not currently available, start a load.
      forceLoad();
    }
  }

  /**
   * Handles a request to stop the Loader.
   *
   * @since 1.0.0
   */
  @Override
  protected void onStopLoading() {
    // Attempt to cancel the current load task if possible.
    cancelLoad();
  }

  /**
   * {@inheritDoc}
   *
   * @param data an object which is handle by this class
   * @since 1.0.0
   */
  @Override
  public void onCanceled(T data) {
    super.onCanceled(data);

    // The load has been canceled, so we should release the resources
    // associated with data.
    onReleaseResources(data);
  }

  /**
   * Handles a request to resetting loader.
   * This will always be called from the process's main thread.
   *
   * @since 1.0.0
   */
  @Override
  protected void onReset() {
    super.onReset();

    // Ensure the loader has been stopped.
    onStopLoading();

    // At this point we can release the resources associated with lastData.
    onReleaseResources(lastData);

    // Stop monitoring for changes.
    unregisterObservers();
  }

  private void onReleaseResources(T data) {
    if (data != null) {
      data = null;
    }
  }
}




Java Source Code List

com.github.vsouhrada.loaders.AbstractCursorLoader.java
com.github.vsouhrada.loaders.AbstractLoader.java
com.github.vsouhrada.loaders.demo.MainActivity.java