1
I wonder how I could pause, cancel this function as soon as I close the Activity that is running.
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
Asynctask you can cancel at any time, just call the Cancel(Boolean) method. It is very important to understand that the task is not canceled immediately and ideally you should always check inside the execution if the task was canceled using the method: isCancelled() As you comment about pausing or canceling when leaving Activity the ideal is to treat inside the onPause() method if you want to stop once you lose focus.
– Marabita