Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid: Async task returns null,even when working
    text
    copied!<p>Good Morning All,</p> <p>I have an Async task that downloads a picture from the internet. I have three buttons with names on them, and if an different name than the one already downloading is pressed, I want it to cancel the task and start one with the new name.</p> <pre><code>DownloadImageTask downloadPicTask = null; int tasksRunning; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { rootView = inflater.inflate(R.layout.detail, container, false); fillData(); return rootView; } public void fillData(){ if(downloadPicTask != null){ Log.i(TAG,"Trying to cancel task..."); downloadPicTask.cancel(true); }else{ Log.i(TAG,"why why why"); } ... downloadPicTask = new DownloadImageTask(imageView); downloadPicTask.execute(firstName,lastName); tasksRunning = tasksRunning + 1; Log.i(tag,String.valueOf(tasksRunning)); ... } private class DownloadImageTask extends AsyncTask&lt;String, Void, Bitmap&gt; { ImageView bmImage; boolean canceled = false; public DownloadImageTask(ImageView bmImage) { this.bmImage = bmImage; } @Override protected void onCancelled() { Log.i(TAG, "Async Cancelled"); canceled = true; } protected Bitmap doInBackground(String... params) { ... //downloading the bitmap here } protected void onPostExecute(Bitmap result) { if(!canceled){ bmImage.setImageBitmap(result); } } } </code></pre> <p>For some reason, once the first task has begun and I press a different name, downloadPicTask returns null, therefore it won't cancel. <strong><em>This happens only the first cancel try!</em></strong> Even thought the task returns null, it runs properly, and sets the imageview when finished. Subsequent tries to cancel the task are succesful, as after the first time downloadPicTask no longer returns null.</p> <p>Anyone have thoughts on this?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload