2

I keep getting the error that my Login Activity has a leaked window of some sort, This is my block of code where the error occurs. I have tried to dismiss both the dialog and the Alert Box respectively, but still, keep getting this error.

    try
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
        builder.setTitle(R.string.login_ok_title);
        builder.setMessage(R.string.login_ok_text).setCancelable(false).setPositiveButton(R.string.ok, (dialog, id) ->
        {
            Intent intent =new Intent(LoginActivity.this, TrackActivity.class);
            startActivity(intent);
            TrackManagerService trackManager =TrackManagerService.getInstance();
            if (trackManager != null) {
                trackManager.setLoginListener(null);
            }

            LoginActivity.this.finish();
            dialog.dismiss();
        });
        alert = builder.create();
        alert.show();

    } 
    catch (Exception ex)
    {
        ex.printStackTrace();
        Intent intent = new Intent(LoginActivity.this, TrackActivity.class);
        alert.dismiss();
        startActivity(intent);
        TrackManagerService trackManager = TrackManagerService.getInstance();
        if (trackManager != null) 
        {
            trackManager.setLoginListener(null);
        }
        finish();
    }

This is the Error Log

E/WindowManager: android.view.WindowLeaked: Activity com.example.tornado_app.LoginActivity has leaked window DecorView@59e20b9[LoginActivity] that was originally added here
    at android.view.ViewRootImpl.<init>(ViewRootImpl.java:627)
    at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:389)
    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:131)
    at android.app.Dialog.show(Dialog.java:531)
    at com.example.tornado_app.LoginActivity$2.lambda$onLoginResult$4$LoginActivity$2(LoginActivity.java:160)
    at com.example.tornado_app.-$$Lambda$LoginActivity$2$YvCiBtxWavxZ1iGtdLspSkuZOdU.run(Unknown Source:6)
    at android.os.Handler.handleCallback(Handler.java:907)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:216)
    at android.app.ActivityThread.main(ActivityThread.java:7625)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)
ibrhm117
  • 387
  • 3
  • 25
  • don't dismiss dialog after starting another activity. Dismiss it before staring another activity. – Sahdeep Singh Feb 18 '20 at 10:09
  • Does this answer your question? [Activity has leaked window that was originally added](https://stackoverflow.com/questions/2850573/activity-has-leaked-window-that-was-originally-added) – Sahdeep Singh Feb 18 '20 at 10:10
  • I did check it, that is when i added the alert.dismiss(); statement but it kept occurring. – ibrhm117 Feb 18 '20 at 10:13

2 Answers2

3

pls try to check if activity is finished or not before showing dialog.

 if (!isFinishing()){
       alert.show();
    }
Manish
  • 1,071
  • 2
  • 10
  • 27
  • Yes, I did do that before but it still kept giving me this issue. – ibrhm117 Feb 18 '20 at 10:55
  • I changed my answer. pls try it once – Manish Feb 18 '20 at 11:19
  • I just did but it still throws this error `Activity com.example.tornado_app.LoginActivity has leaked window DecorView@aeec1d9[LoginActivity] that was originally added here at android.view.ViewRootImpl.(ViewRootImpl.java:627) at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:389) at com.example.tornado_app.LoginActivity$2.lambda$onLoginResult$4$LoginActivity$2(LoginActivity.java:160)` – ibrhm117 Feb 18 '20 at 11:45
  • thank you this worked well , I tried to rebuild and clean the project and then ran it again. – ibrhm117 Feb 19 '20 at 05:20
2

Why are you use 2 time dialog.dismiss(); and after activity finish how you can dismiss dialog?Try to add below code

try {
    AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
    builder.setTitle(R.string.login_ok_title);
    builder.setMessage(R.string.login_ok_text).setCancelable(false).setPositiveButton(R.string.ok, (dialog, id) ->
    {
        //dialog.dismiss(); //Remove this
        Intent intent = new Intent(LoginActivity.this, TrackActivity.class);
        startActivity(intent);

        TrackManagerService trackManager = TrackManagerService.getInstance();
        if (trackManager != null) {
            trackManager.setLoginListener(null);
        }

        LoginActivity.this.finish();
        //dialog.dismiss(); //Remove this
    });
    alert = builder.create();
    alert.show();

} catch (Exception ex) {
    ex.printStackTrace();
    Intent intent = new Intent(LoginActivity.this, TrackActivity.class);
    startActivity(intent);
    TrackManagerService trackManager = TrackManagerService.getInstance();
    if (trackManager != null) {
        trackManager.setLoginListener(null);
    }
    finish();
}

I hope this can help you!

Hardik Talaviya
  • 1,396
  • 5
  • 18