Unable to dismiss the progressdialog inside the addonsucesslistener() which is inside “for loop”

I’m selecting multiple images and storing them into Firebase Storage, and retrieving the download URL inside an ArrayList and adding it into the Firebase Database, my code is working fine but I don’t know when to stop the progress dialog, where I have to dismiss the progress dialog so when the task of the progress bar is completed than progress dialog disappear.

Here is my code:

private void uploadScreenshot() {

    if (screenshotUri != null) {
        final ProgressDialog mDialog = new ProgressDialog(this);

        String ssName = UUID.randomUUID().toString();
        final StorageReference screenshotFolder = storageReference.child("screenshots/" + ssName);

        for (upload_count = 0; upload_count < ScreenshotList.size(); upload_count++) {
            Uri IndividualImage = Uri.parse(ScreenshotList.get(upload_count));
            final StorageReference ScreenshotName = screenshotFolder.child(IndividualImage.getLastPathSegment());

            ScreenshotName.putFile(IndividualImage).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    ScreenshotName.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                        public void onSuccess(Uri uri) {
                            String url = String.valueOf(uri);
            }).addOnFailureListener(new OnFailureListener() {
                public void onFailure(@NonNull Exception e) {
                    Toast.makeText(AppList.this, "" + e.getMessage(), Toast.LENGTH_SHORT).show();
                    .addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
                        public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
                            double progress = (100.0 * taskSnapshot.getBytesTransferred() / taskSnapshot.getTotalByteCount());
                            mDialog.setMessage("Uploaded " + progress + "%");

    // mDialog.dismiss();
    //Toast.makeText(AppList.this, "Screenshots Uploaded !!!", Toast.LENGTH_SHORT).show();

private void storeLink(String url) {




When you call the uploadScreenshot() the first thing you do is to display a ProgressDialog. When you upload a file to Firebase Storage, the response that you get can be either a success or a failure, never both. This means that you should dismiss the dialog in both onSuccess() and onFailure() methods. If you have successfully uploaded the file, the dialog should be dismissed, as it also should be if you get a failure. So please add the following lines of code:


Inside onSuccess() and onFailure() methods.


If you want to dismiss the dialog only after uploading all files, then you should use recursion as explained in my answer from the following post:

This means that when the size of the list becomes zero, you can dismiss the dialog.