just_user
just_user

Reputation: 12059

show() progressdialog in asynctask inside fragment

I'm trying to display a progressDialog while getting an image from a url to a imageview. When trying to show the progressDialog the parent activity has leaked window...

Strange thing is that I have two fragments in the this activity, in the first fragment this exact same way of calling the progressdialog works but when the fragment is replaced and i try to make it again it crashes.

This is the asynctask I'm using inside the second fragment with the crash:

class SkinPreviewImage extends AsyncTask<Void, Void, Void> {

    private ProgressDialog progressDialog;

    @Override
    protected void onPreExecute() {
        progressDialog = new ProgressDialog(getActivity());
        progressDialog.setMessage("Loading preview...");
        if(progressDialog != null) progressDialog.show();
        progressDialog.setOnCancelListener(new OnCancelListener() {
            public void onCancel(DialogInterface arg0) {
                SkinPreviewImage.this.cancel(true);
            }
        });
    }

    @Override
    protected Void doInBackground(Void... params) {
        try {
            URL newurl = new URL(url); 
            Bitmap mIcon_val = BitmapFactory.decodeStream(newurl.openConnection().getInputStream());
            skinPreview.setImageBitmap(mIcon_val);
        } catch (IOException e) {
            Log.e("SkinStoreDetail", e.getMessage());
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void v) {
        if(progressDialog != null) progressDialog.dismiss();
    }
}

I've seen a few similar questions but the one closest to solve my problem used a groupactivity for the parent which I'm not using.

Any suggestions?

Upvotes: 1

Views: 7891

Answers (1)

Marcin Orlowski
Marcin Orlowski

Reputation: 75629

You cannot do any UI task in doInBackground() or you will crash. Override onProgressUpdate() and do that there. See docs for more details: http://developer.android.com/reference/android/os/AsyncTask.html

Upvotes: 1

Related Questions