1

I want to show prograssbar with the help of Alert.Dialog while saving data to database. but after registration the program crashes.

A method that gives an error after running:

private void saveData() {

        if (isPickPhoto) {
            StorageReference mRef = FirebaseStorage.getInstance().getReference().child("ShopList Images")
                    .child(imgUri.getLastPathSegment());
            AlertDialog.Builder builder = new AlertDialog.Builder(AddShopListActivity.this);
            builder.setCancelable(false);
            builder.setView(R.layout.progress_layout);
            AlertDialog dialog = builder.create();
            dialog.show();

            mRef.putFile(imgUri).addOnSuccessListener(taskSnapshot -> {
                Task<Uri> uriTask = taskSnapshot.getStorage().getDownloadUrl();
                while (!uriTask.isComplete()) ;
                Uri urlImage = uriTask.getResult();
                imageUrl = urlImage.toString();
                uploadData();
                dialog.dismiss();
            }).addOnFailureListener(e -> {
                dialog.dismiss();
            });
        } else {
            Toast.makeText(this, "Yükleme yapmak için galeriden bir resim seç", Toast.LENGTH_SHORT).show();
        }
    }

but when I use it to update the data it works fine.

method that works flawlessly:

private void updateData() {
        AlertDialog.Builder builder = new AlertDialog.Builder(AddShopListActivity.this);
        builder.setCancelable(false);
        builder.setView(R.layout.progress_layout);
        AlertDialog dialog = builder.create();
        dialog.show();

        if (isPhotoChanged) {

            StorageReference storageReference = FirebaseStorage.getInstance().getReference().child("ShopList Images").child(imgUri.getLastPathSegment());

            storageReference.putFile(imgUri).addOnSuccessListener(taskSnapshot -> {
                Task<Uri> uriTask = taskSnapshot.getStorage().getDownloadUrl();
                while (!uriTask.isComplete()) ;
                Uri urlImage = uriTask.getResult();
                imageUrl = urlImage.toString();
                uploadData();
                dialog.dismiss();
            }).addOnFailureListener(e -> {
                Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
            });
        } else {
            imageUrl = oldImageUrl;
            uploadData();
            dialog.dismiss();
        }
    }

what can i do for using AlertDialog in saveData() metod ?

i want to show progressbar with Dialog.Alert.builder. but it crashes after data saved.

LogCat screen:

enter image description here

gkhnmr
  • 13
  • 3

2 Answers2

2

This particular exception seems to be a bit tricky to interpret, but here's a good explanation of what can cause this error. tl;dr: For your case the error appear to be the dialog is outliving the Activity it is called in, because the .dismiss()-call isn't being issued properly sometimes.

Now it seems to be a bit tricky to narrow down the root, when the cause of your Activity shutting down prematurely won't show up in the logs. (There are reports of observing that in the comments under a couple of the answers of the SO post above.)
So to find out what could be shutting down your method before the dialog could be dismissed, comment out the dialog.show(), reproduce the steps that caused this error to occur and check the logs again.

pflakus
  • 99
  • 6
  • awesome. thanks for the answer. onSuccessListener lives longer than main algorithm. I changed .dismis() position their places to be before the finish. now all is working. – gkhnmr Apr 21 '23 at 13:54
1

The WindowLeaked error occurs when you try to show a dialog after the activity has finished. To fix this issue, use isFinishing() to check if the activity is still running before showing the dialog:

private void saveData() {
    if (isPickPhoto) {
        //...
        if (!AddShopListActivity.this.isFinishing()) {
            dialog.show();
        }

        mRef.putFile(imgUri).addOnSuccessListener(taskSnapshot -> {
            //...
            if (!AddShopListActivity.this.isFinishing()) {
                dialog.dismiss();
            }
        }).addOnFailureListener(e -> {
            if (!AddShopListActivity.this.isFinishing()) {
                dialog.dismiss();
            }
        });
    } else {
        //...
    }
}

This will prevent the crash by ensuring the dialog is only shown and dismissed when the activity is active.

Saurabh
  • 164
  • 2