Sam Borick
Sam Borick

Reputation: 955

Issues with calling AsyncTask on Button press

I am working on an app which requires that I download an image after a button press. I was going to use AsyncTask, until I found out that you can only call a particular AsyncTask once. What should I use instead so that I can still use Progress Dialog and whatnot but still call it on button press?

called on button press, passing in an int

class ImageDownloader
        extends AsyncTask<Integer, Integer, Bitmap> {
    protected void onPreExecute(){
        launchDialog();
    }

    @Override
    protected Bitmap doInBackground(Integer... params) {
        //TODO Auto-generated method stub
        try{
            //finding and downloading an image, and passing back the proper bitmap to the onPostExecute
        }catch(Exception e){
            Log.e("Image", "Failed to load image", e);
        }
        return null;
    }
    protected void onProgressUpdate(Integer... params){

    }
    protected void onPostExecute(Bitmap img){
        ImageView iv = (ImageView) findViewById(R.id.imageView);
        if(iv!=null && img!=null){
            iv.setImageBitmap(img);
            new PhotoViewAttacher(iv);
        }
        closeDialog();
        enablebuttons();
    }


    protected void onCancelled(){
        closeDialog();
        enablebuttons();
    }
}

Thanks in advance!

Upvotes: 0

Views: 163

Answers (1)

kabuko
kabuko

Reputation: 36302

Just create a new instance of the AsyncTask in your click handler every time and run that (as opposed to executing a single instance over and over).

Upvotes: 1

Related Questions