32

I am getting window leak error at runtime because of using an AlertDialog.

I have pointed out the error line in the code below:

Stacktrace:

08-18 02:48:04.489  28893-28893/? E/WindowManager﹕ Activity com.ms.ha.fragment.FirstActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{52e58540 V.E..... R.....ID 0,0-1026,585} that was originally added here
    android.view.WindowLeaked: Activity com.ms.ha.fragment.FirstActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{52e58540 V.E..... R.....ID 0,0-1026,585} that was originally added here
            at android.view.ViewRootImpl.<init>(ViewRootImpl.java:345)
            at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:239)
            at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
            at android.app.Dialog.show(Dialog.java:281)
            at com.ms.ha.fragment.TourGuideLoadURLFragment$WebAppInterface.moveToNextScreen(TourGuideLoadURLFragment.java:116)
            at android.webkit.WebViewCore.nativeMouseClick(Native Method)
            at android.webkit.WebViewCore.nativeMouseClick(Native Method)
            at android.webkit.WebViewCore.access$6800(WebViewCore.java:59)
            at android.webkit.WebViewCore$EventHub.dispatchWebKitEvent(WebViewCore.java:1793)
            at android.webkit.WebViewInputDispatcher.dispatchWebKitEvent(WebViewInputDispatcher.java:689)
            at android.webkit.WebViewInputDispatcher.dispatchWebKitEvents(WebViewInputDispatcher.java:639)
            at android.webkit.WebViewInputDispatcher.access$800(WebViewInputDispatcher.java:78)
            at android.webkit.WebViewInputDispatcher$WebKitHandler.handleMessage(WebViewInputDispatcher.java:1153)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.webkit.WebViewCore$WebCoreThread.run(WebViewCore.java:814)
            at java.lang.Thread.run(Thread.java:841)

FirstActivity.java:

 public class FirstActivity extends FragmentActivity  implements View.OnClickListener{

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.fragment_tour_guide_web);
  .......
  .......

   webView.addJavascriptInterface(new WebAppInterface(this), "Android");

 }


    public class WebAppInterface {
        Context mContext;

        /**
         * Instantiate the interface and set the context
         */
        WebAppInterface(Context c) {
            mContext = c;
        }

        /**
         * Intent - Move to next screen
         */

        @JavascriptInterface
        public void moveToNextScreen() {
            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                    context);

            // set title
            alertDialogBuilder.setTitle("Your Title");

            // set dialog message
            alertDialogBuilder
                    .setMessage("Click yes!")
                    .setCancelable(false)
                    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {

                            Intent i = new Intent(FirstActivity.this,SecondActivity.class);
                            startActivity(i);
                        }
                    })
                    .setNegativeButton("No", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            // if this button is clicked, just close
                            // the dialog box and do nothing
                            dialog.cancel();
                        }
                    });

            // create alert dialog
            AlertDialog alertDialog = alertDialogBuilder.create();

            // show it
            alertDialog.show();  --->leak window error

        }

    }

  }

I don't know how to solve this issue.

Adinia
  • 3,722
  • 5
  • 40
  • 58
Steve
  • 1,153
  • 2
  • 15
  • 29
  • 1
    http://stackoverflow.com/questions/2850573/activity-has-leaked-window-that-was-originally-added Check out this. – Beena Aug 18 '15 at 07:14
  • 2
    It occurs when you try to show / dismiss an ALertDialog created and showing in one Activity and dismissing in another Activity. – Logic Aug 18 '15 at 07:16

6 Answers6

60

You get error because ProgressDialog is running while your Activity is destroyed. You should dismiss dialog before starting new Activity.

.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
  public void onClick(DialogInterface dialog, int id) {
      if (alertDialog != null && alertDialog.isShowing()) {
          alertDialog.dismiss();
      }
      Intent i = new Intent(FirstActivity.this, SecondActivity.class);
      startActivity(i);
  }
});

I hope it helps!

Rajesh Jadav
  • 12,801
  • 5
  • 53
  • 78
  • Try code i have edited in answer. dismiss dialog or finish activity when new activity starts – Rajesh Jadav Aug 18 '15 at 07:29
  • That means that your main thread doesn't know about your changes and then when it takes action your main thread finds that it is already different object. try call your function in main thread.` runOnUiThread(new Runnable() { @Override public void run() { //your function } });` – Jevgenij Kononov Mar 23 '17 at 10:26
  • but in that case, stacktrace should have leak on `onStop()` or `onDestroy()`, isn't so? – kAmol Jan 29 '19 at 06:25
16

Dismiss the alertDialog in onPause() method. i.e. call alertDialog.dismiss()

Note : WindowLeaked exceptions are occured usually if dialogs are not dismissed before Activity is ended.

N Kaushik
  • 2,198
  • 18
  • 29
5

You can also get a WindowLeaked exception if you're not in the UI thread when attempting to show the dialog. Easy fix:

this.runOnUiThread(new Runnable() {
    @Override
    public void run() {
        // show dialog here
    }
});
ScottyB
  • 2,167
  • 1
  • 30
  • 46
3

You must need to dismiss/cancel your dialog once activity is finishing. It occurs usually if dialogs are not dismissed before Activity is ended.

Daud Arfin
  • 2,499
  • 1
  • 18
  • 37
-12

Check in your manifest if you have setted internet permissions:

<uses-permission android:name="android.permission.INTERNET" />
-14

I don't know does this help but at me problem's were causing multidex. At build.gradle

multiDexEnabled false
Terhoraj
  • 265
  • 4
  • 18