###AsyncTask#cancel
が上手く作動しなくて困っています。
AsyncTaskを保持するActivityを終了した時にAsyncTaskのBackground処理を中断させようと思い、AsyncTask#cancel(true)
を使ってみたのですが、ログを見るとどうにも中断できていないようです。。。
Activity
lang
1public class ProfessorListActivity extends Activity { 2 Context context = this; 3 /** ListView内要素を検索するSearchView */ 4 SearchView searchView; 5 /** Professorのカテゴリを要素として保持するListView */ 6 ListView listView; 7 /**Professorの属性を保持するProfessorDataを扱うList*/ 8 public ArrayList<ProfessorData> professorDataList = new ArrayList<>(); 9 /**ProfessorDataを扱うAdapter*/ 10 ProfessorListAdapter adapter; 11 12 @Override 13 protected void onCreate(Bundle savedInstanceState) { 14 //searchViewとListViewの実装、ProfessorDataListの生成 15 ----------処理------------------------ 16 adapter = new ProfessorListAdapter(context, R.layout.custom_cell, professorDataList); 17 listView.setAdapter(adapter); 18 ----------処理------------------------ 19 } 20 21 @Override 22 protected void onDestroy() { 23 //if (adapter.task != null) { 24 if (!adapter.task.isCancelled()) { 25 Log.d(CLASS_TAG, "onDestroy()!!!!"); 26 adapter.task.cancel(true); 27 } 28 super.onDestroy(); 29 } 30}
Adapter
lang
1public class ProfessorListAdapter extends ArrayAdapter<ProfessorData> { 2 /** 呼び出し元であるActivityのContext */ 3 private Context _context; 4 /** 取得したListViewCellに要素を挿入するためのLayoutInflater */ 5 private LayoutInflater _inflater; 6 /** 指定されたListViewCellId */ 7 private int _textViewResourceId; 8 /** ListView生成時の初期要素を保持するList */ 9 private List<ProfessorData> initialEntry; 10 /** 検索Filter */ 11 private MenuFilter _filter; 12 /** 呼び出し元のActivityに渡すため*/ 13 public DownloadTask task; 14 15 public ProfessorListAdapter(Context context, int resource, ArrayList<ProfessorData> objects) { 16 super(context, resource, objects); 17 _context = context; 18 _inflater = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 19 _textViewResourceId = resource; 20 } 21 22----------------------------処理------------------------- 23 24 //ここで非同期処理 25 @Override 26 public View getView(int position, View convertView, ViewGroup parent) { 27 ItemLayout view; //ViewHolderの代わり 28 final ProfessorData item = (ProfessorData) getItem(position); 29 if (convertView == null) { 30 view = (ItemLayout) _inflater.inflate(_textViewResourceId, null); 31 } else { 32 view = (ItemLayout) convertView; 33 } 34 view.mThumbNailView.setVisibility(View.GONE); //ListViewCell画像 35 view.mThumbNailView.setTag(item.imageString); 36 task = new DownloadTask(_context, view.mThumbNailView, item); 37 task.execute(item.imageString); 38 view.mNameView.setText(item.name); //ListViewCellText 39 return view; 40 } 41}
AsyncTask
lang
1public class DownloadTask extends AsyncTask<String, Void, Bitmap> { 2 3 private Context mContext; 4 private ImageView mImageView; 5 private String mTag; 6 private ProfessorData item; 7 8 public DownloadTask(Context context, ImageView imageView, ProfessorData item) { 9 this.mContext = context; 10 this.mImageView = imageView; 11 this.mTag = imageView.getTag().toString(); 12 this.item = item; 13 } 14 15 @Override 16 protected Bitmap doInBackground(String... imgStrs) { 17 synchronized (mContext) { 18 if (isCancelled()) { 19 Log.d("task#doInBackground", "Cancelled!!"); 20 return null; 21 } else { 22 if (item.imageString.equals(imgStrs[0])) 23 Log.d("task#doInBackground", "name -> " + String.valueOf(item.name) + ", imageStr" + String.valueOf(item.imageString)); 24 try { 25 NCMBFile file = new NCMBFile((String) imgStrs[0]); 26 byte[] bytes; 27 try { 28 bytes = file.fetch(); 29 } catch (Exception e) { 30 bytes = null; 31 } 32 Bitmap image = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); 33 if (image == null) { 34 try { 35 image = UoASetting.loadBitmapFromAsset("res/img/TopActivity/info_button.png"); 36 } catch (Exception e) { 37 Log.d("Exception type", String.valueOf(e.getClass())); 38 Log.d("task#doInBackground", "UoASetting#loatBitmapFromAsset: " + String.valueOf(e)); 39 } 40 } 41 return image; 42 } catch (Exception e) { 43 Log.d("Exception type", String.valueOf(e.getClass())); 44 Log.d("task#doInBackground", "task#doInBackground: " + String.valueOf(e)); 45 } 46 return null; 47 } 48 } 49 } 50 51 @Override 52 protected void onPostExecute(Bitmap img) { 53 if (this.mTag.equals(this.mImageView.getTag())) { 54 if (img != null) { 55 this.mImageView.setImageBitmap(img); 56 this.mImageView.setVisibility(View.VISIBLE); 57 } 58 } 59 } 60}
一応無駄な部分はできるだけ消しましたが、長くなってしまい申し訳ないです。AsyncTaskのコードだけはほぼそのままにしておきました。
自分的にはDownloadTaskのオブジェクトを親に渡している所が原因な気がするのですが、どうすれば良いのかが思いつきません。
皆様宜しくお願い致します。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/09/19 06:28 編集