0

when trying to navigate to next activity after Alert Dialog, then I am getting

this error:

Activity has Leaked window DecorView@a61b0ed[] that was originally addded here

here is the snippet:

if (alert1 != null && alert1.isShowing()) {
    alert1.dismiss();
}

builder.setCancelable(true);
final AlertDialog alert1 = builder.create();
alert1.show();

onPause();

final Timer t = new Timer();
t.schedule(new TimerTask() {
    @Override
    public void run() {
        alert1.dismiss();
        t.cancel();
    }
}, 3000);

if (updatedQnty.equals("order full")) {

    Intent intent = new Intent();
    setResult(Activity.RESULT_OK, intent);
    finish();
   // callForDestroy(alert1);

}else{
    mScannerView.resumeCameraPreview(this);
}
Komal12
  • 3,340
  • 4
  • 16
  • 25
202207
  • 17
  • 1
  • Your activity is getting destroyed before you dialog is dismissed. So you have to dismiss your alert before the activity gets destroyed. – Paresh P. Mar 22 '18 at 07:05
  • 1
    Possible duplicate of [Activity has leaked window that was originally added](https://stackoverflow.com/questions/2850573/activity-has-leaked-window-that-was-originally-added) – Micer Mar 22 '18 at 07:06

3 Answers3

0

The error means you're trying to show or dismiss dialog after activity was finished. You should dismiss it before the parent activity is destroyed.

Check activity lifecycle and make sure your activity still exists when you call alert1.show(); or alert1.dismiss();

Micer
  • 8,731
  • 3
  • 79
  • 73
0

You must dismiss a showing AlertDialog before navigating to another Activity. You could add your check-and-dismiss function on Activity:onStop() to ensure you dismiss it.

Seop Yoon
  • 2,429
  • 3
  • 14
  • 18
0

Your problem probably because your timer is still running but the activity is already finished. So, you need to make sure to close the dialog before the activity is finished.

You can solve the problem by cancelling the timer when the activity is finished. Something like this:

if (updatedQnty.equals("order full")) {

    Intent intent = new Intent();
    setResult(Activity.RESULT_OK, intent);

    // cancel the timer so the dialog not shown.
    t.cancel();
    // now you can finish the activity.
    finish();
}
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96