AsyncTaskLoaderに手を出してみる (d:id:Nkzn:20120113:1326478800) ã®ç¶ãã
ã¨ããããï¼ã¶æã»ã©AsyncTaskLoaderããã¨ä»²è¯ããã¦ã¿ã¾ããã
è²ã
ãã£ã¦ãAsyncTaskããã¨ãå°ãã ã仲è¯ããªãã¾ããã
ã¨ãããã¨ã§ãç¾å¨ã®åã®AsyncTaskLoaderã®ä½¿ãæ¹ãã¡ã¢ãã¦ããã¾ãã
public class AsyncSampleLoader extends AsyncTaskLoader<String> { private static final String tag = "AsyncSampleLoader"; /** * å¼ã³åºãå ããã®å¤æ¸¡ãç¨ãã£ã¼ã«ã */ List<Hoge> mHogeParams; String result; // å®å public AsyncSampleLoader(Context context) { super(context); mHogeParams = new ArrayList<Hoge>(); } public AsyncSampleLoader(Context context, Hoge hogeParams) { super(context); mHogeParams = hogeParams; } @Override public String loadInBackground() { String data = hogehogeAccessNetwork(mHogeParams); // éåæã«ãããå¦çæ¬ä½ return data; } /* ============以ä¸ãresultãdataã®å以å¤ã¯å®å============ */ @Override public void deliverResult(String data) { if (isReset()) { if (this.result != null) { this.result = null; } return; } this.result = data; if (isStarted()) { super.deliverResult(data); } } @Override protected void onStartLoading() { if (this.result != null) { deliverResult(this.result); } if (takeContentChanged() || this.result == null) { forceLoad(); // ããããã£ã¦ããã¨onCreateLoaderã§éå§å¦çãããªãã¦ãããªã } } @Override protected void onStopLoading() { super.onStopLoading(); cancelLoad(); } @Override protected void onReset() { super.onReset(); onStopLoading(); } @Override public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) { super.dump(prefix, fd, writer, args); writer.print(prefix); writer.print("result="); writer.println(this.result); } }
çµå± 公式のサンプルã¨åãæãã«ãªãã¾ããã
èªåã§æ¸ãã¦ãã®ã¯ã³ã³ã¹ãã©ã¯ã¿ã¨loadInBackgroundãããããåOverrideã¡ã½ããå
ã®åãã©ã¡ã¼ã¿ä¾åã®åã ãã§ãã
å¼ã³åºãé¨åã®ã³ã¼ããå°ãã ãæ¸ãã¦ããã¾ããï¼Activityå ã§ã®å®è¡ãæ³å®ï¼
void startLoader() { getLoaderManager().initLoader(LOADER_ID, null, this); // ã³ã³ããã®å ´åã¯getSupportLoaderManager().initLoader(LOADER_ID, null, this); } @Override public Loader<List<String>> onCreateLoader(int id, Bundle args) { return new AsyncSampleLoader(this, mHogeParams); // ã¾ã㯠new AsyncSampleLoader(this); } @Override public void onLoadFinished(Loader<List<String>> loader, List<String> data) { /* dataã使ã£ãå¦ç */ // restartLoaderã®ä½¿ãæ¹ãã¾ã ããåãã£ã¦ãªãã®ã§ã次åã®èªã¿è¾¼ã¿ã§ã¾ãinitLoaderã使ããããã«ä¸åº¦æ®ºãã¦ããã getLoaderManager().destroyLoader(loader.getId()); // loader.getId() == LOADER_ID ã®ã¯ã // ã³ã³ããã®å ´åã¯getSupportLoaderManager().destroyLoader(loader.getId()); } @Override public void onLoaderReset(Loader<List<String>> loader) { }
âãæ¸ããªããrestartLoaderã®ä½¿ãæ¹ã調ã¹ã¦ããã公式のLoaderManagerのページã«ä½¿ãæ¹ã£ã½ããã®ãè¼ã£ã¦ã¦ãã¡ãã£ã¨ç解ãå§ãã¾ããã
ã¤ãã§ã«ä»ã¾ã§ã¤ããã ã£ãonLoaderResetã®ä½¿ãéãããããåãã£ãæ°ããã¾ãã
å®ç¨ãã¦ã¿ã¦ä½¿ãæ¹ãã¤ããããã¾ãè¨äºãæ¸ããã¨æãã¾ãã
ããããªåã®LoaderCallbacksãå®è£ ãããå ´å
ä»åç´¹ä»ããã®ã¯Stringã§ããããï¼ã¤ã®ActivityãFragmentã®ä¸ã§è¤æ°ã®åã®ãã¼ã¿ãéåæã«èªã¿è¾¼ã¿ããå ´åã£ã¦ããã¾ãããã
ãããªå ´åã®ããããã¯ããã¾ããå
çï¼çºæ³å
ã¯ããããå
çã ãã©ï¼ãç´¹ä»ãã¦ããã¦ããã®ã§ããã¡ãã使ã£ã¦ãã ããã
ï¼ç»é¢ã§è¤æ°AsyncTaskLoader
http://blog.livedoor.jp/ryosms/archives/5712827.html
ãã®ããæ¹ãªããï¼ã¤ã¨è¨ããããã¤ã§ã対å¿ã§ãããã§ããã
ã¨ããããä»åã¯ããã¾ã§ã