
AsyncTaskLoader | Android Developers
目次
1. Thread と AsyncTask
2. AsyncTaskLoader の利点
3. LoaderManager の利用
4. よくある間違いと回避法
5. 基本的な Loader の実装
6. いろいろな Loader の使用例
7. データベースと CursorLoader
8. Loader の弱点を克服する
5. 基本的な Loader の実装
・ Loader
ベースとなる抽象化クラス。
・ AsyncTaskLoader
Loader の拡張。
・ CursorLoader
AsyncTaskLoader の拡張。
ContentsProvider 専用。
AsyncTaskLoader の制限事項
private static final int CORE_POOL_SIZE = 5;
private static final int MAXIMUM_POOL_SIZE = 128;
private static final int KEEP_ALIVE = 1;
public static final Executor THREAD_POOL_EXECUTOR
= new ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE,
KEEP_ALIVE, TimeUnit.SECONDS, sPoolWorkQueue, sThreadFactory);
private static volatile Executor sDefaultExecutor = THREAD_POOL_EXECUTOR;
内部的に ModernAsyncTask を使用している。
重要: ローダーにアプリケーションのコンテキストの参照のみを保持させることでリークを避ける。
/**
* コンテキストに関連付けられているアプリケーション·コンテキストを格納する。
* ローダーは複数のアクティビティで使用できるので、直接コンテキストを保存することは危険。
*
* @param context アプリケーション・コンテキストを取得するために使用
*/
public Loader(Context context) {
mContext = context.getApplicationContext();
}
Loader 内部クラスの各々は、staticとして宣言されなければならない、さもなければ親への暗黙の参照を維持してしまう。
AsyncTaskLoaderを拡張して実装する。
必須なコールバック
* onStartLoading() * onStopLoading() * onReset() * onForceLoad() from Loader / loadInBackground() from AsyncTaskLoader
オプション
* deliverResult() [override]
public abstract class BasicLoader<T> extends AsyncTaskLoader<T> {
public BasicLoader(Context context) {
super(context);
}
@Override
protected void onStartLoading() {
forceLoad(); // Launch the background task
}
@Override
protected void onStopLoading() {
cancelLoad(); // Attempt to cancel the current load task if possible
}
@Override
protected void onReset() {
super.onReset();
onStopLoading();
}
}
public abstract class LocalCacheLoader<T> extends AsyncTaskLoader<T> {
private T mResult;
public AbstractAsyncTaskLoader(Context context) {
super(context);
}
@Override
protected void onStartLoading() {
if (mResult != null) {
// If we currently have a result available, deliver it
// immediately.
deliverResult(mResult);
}
if (takeContentChanged() || mResult == null) {
// If the data has changed since the last time it was loaded
// or is not currently available, start a load.
forceLoad();
}
}
...
@Override
protected void onStopLoading() {
// Attempt to cancel the current load task if possible.
cancelLoad();
}
@Override
protected void onReset() {
super.onReset();
onStopLoading();
mResult = null;
}
@Override
public void deliverResult(T data) {
mResult = data;
if (isStarted()) {
// If the Loader is currently started, we can immediately
// deliver its results.
super.deliverResult(data);
}
}
}
(→「6. いろいろな Loader の使用例」につづく)
関連ワード: Android・初心者・開発・AsyncTaskLoader・LoaderManager