1

When I run below codes I get this error message : "020-11-07 18:45:26.737 20684-20684/com.user.example E/WindowManager: android.view.WindowLeaked: Activity com.user.example.Pages.Dashboard.Home has leaked window DecorView@14fe64f[Home] that was originally added here" but my application still run without crash. How can I fix that problem _

These codes belong to an activity.

  public void login(View v) {
    if( !validateUserEmail() | !validatePassword()){
        return;
    }
    UserInfo userInfo = new UserInfo(mail,password);
    SendRequestToAPI.sendRequest(userInfo,Login.this);
}

These codes belong to a class which name is SendRequestToAPI.

public static void showWarningAlert(final Context context, String title, 
String contentText, String confirmText){
        SweetAlertDialog alertDialog = new 
        SweetAlertDialog(context,SweetAlertDialog.WARNING_TYPE);
        alertDialog.setTitle(title);
        alertDialog.setContentText(contentText);
        alertDialog.setConfirmText(confirmText);
        alertDialog.setConfirmClickListener(new 
        SweetAlertDialog.OnSweetClickListener() {
            @Override
            public void onClick(SweetAlertDialog sweetAlertDialog) {
                Intent intent = new Intent(context, Login.class);
                context.startActivity(intent);
                ((Activity)context).finish();
            }
        }).setCancelButton("Cancel", new 
        SweetAlertDialog.OnSweetClickListener() {
                    @Override
                    public void onClick(SweetAlertDialog sweetAlertDialog) {
                        sweetAlertDialog.dismissWithAnimation();
                    }
                })
                .show();

    }

2 Answers2

0

This happens when you have an alert window but the activity destroyed or exited..In other words, if you have an alert window, and the activity which gives you the context is destroyed or stopped already due to some logic in your code.

Makes sure whenever your activity is destroyed, your alert dialog is dismissed as well.. you can write this logic in your activity's destroy()method and onStop method()..alert dialog can only exist and work without any leak as soon as your associated activity is alive.

You may also implement ot other way by maing sure that whenever you show alertdialog , your activity is not stopped or destroyed.

Implement however you want to but onPause() and onDestroy() is the key.

Rishabh Ritweek
  • 550
  • 3
  • 10
  • edited my answer..make sure you dont show any alert dialog when activity is exited or destroyed – Rishabh Ritweek Nov 07 '20 at 16:22
  • just call alertDialog.dismiss() from onPause and onDestroy of the activity if you have already called alertDialog.show()...understand the reason, you cant show an alert dialog if your activity is destroyed or stopped. – Rishabh Ritweek Nov 07 '20 at 16:43
0

When I added an if block to this code stream, my error was resolved.

 public void onClick(SweetAlertDialog 
 sweetAlertDialog) {
            if (sweetAlertDialog != null && 
 sweetAlertDialog.isShowing()) {
                sweetAlertDialog.dismiss();
            }
            Intent intent = new Intent(context, 
 Login.class);
            context.startActivity(intent);
            ((Activity)context).finish();
        }