一、问题
E/WindowManager: android.view.WindowLeaked: Activity com.jeany.demo.MainActivity has leaked window [email protected][] that was originally added here at android.view.ViewRootImpl.<init>(ViewRootImpl.java:485) at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:346) at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:92) at android.app.Dialog.show(Dialog.java:330) at com.jeany.base.ui.widget.SdkAlertDialog.show(SdkAlertDialog.java:26) at com.jeany.sdk.WYSdk.handleBackAction(WYSdk.java:105) at com.jeany.view.login.BaseLoginDialog$BackListener.onKey(BaseLoginDialog.java:125) at android.app.Dialog.dispatchKeyEvent(Dialog.java:776) at com.android.internal.policy.DecorView.dispatchKeyEvent(DecorView.java:351) at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:4714) at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4586) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4128) at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4181) at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4147) at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4274) at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4155) at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4331) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4128) at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4181) at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4147) at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4155) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4128) at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4181) at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4147) at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4307) at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:4468) at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:2435) at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1998) at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1989) at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:2412) at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141) at android.os.MessageQueue.nativePollOnce(Native Method) at android.os.MessageQueue.next(MessageQueue.java:325) at android.os.Looper.loop(Looper.java:142) at android.app.ActivityThread.main(ActivityThread.java:6541) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
二、分析原因
Android 的每一个 Activity 都有个 WindowManager 窗体管理器,
同样,构建在某个 Activity 之上的对话框、PopupWindow 也有相应的 WindowManager 窗体管理器。
因为对话框、PopupWindown 不能脱离 Activity 而单独存在着,
所以当某个 Dialog (或 PopupWindow) 正在显示的时候去 finish() 了承载该 Dialog (或 PopupWindow )的 Activity 时,就会抛 Window Leaked 异常了,
因为这个 Dialog (或 PopupWindow)的WindowManager已经没有谁可以附属了,所以它的窗体管理器已经泄漏了。
android.view.WindowLeaked 一般会发生在 Activity 与 Dialog 的显示。
Activity 中 create 一个 Dialog ,若先关闭 Dialog 再关闭 Activity 就是正常的,
若先关闭 Activity 再关闭Dialog 就会报错这个android.view.WindowLeaked错误了。
分析:Dialog 是基于 Activity 而创建的:new ProgressDialog(this);this 就是 Activity。
Activtity 先 finish, 那 Dialog 就没得依附了,所以就会报 android.view.WindowLeaked。
三、解决方案
关闭 (finish) 某个 Activity 前,要确保附属在上面的 Dialog 或 PopupWindow已经关闭 (dismiss) 了。
四、参考文档
https://blog.csdn.net/xuan521521/article/details/51878403
原文地址:https://www.cnblogs.com/pinweyshg/p/12005973.html