alertdialog旋转屏幕消失造成leak window

在activity里边创建AlertDialog并显示它,此时转屏,发现对话框会消失,Activity会被重新创建。
即:
onCreate -> onStart-> onResume ->旋转 -> onPause -> onStop -> onDestroy -> onCreate -> onStart -> onResume

效果看起来很好,但是从log,发现似乎发生溢出。

05-12 10:05:34.418 11242 11242 D TAG     : getProp: unknown
05-12 10:05:34.442 11242 11289 D Surface : Surface::disconnect(this=0x71ce5cb000,api=1)
05-12 10:05:34.449 11242 11242 D View    : [Warning] assignParent to null: this = DecorView@459eb07[MainActivity]
05-12 10:05:34.465 11242 11242 E WindowManager: 
05-12 10:05:34.465 11242 11242 E WindowManager: android.view.WindowLeaked: Activity dsfingerprint.dy.txx.com.dsfingerprint.MainActivity has leaked window DecorView@687d778[MainActivity] that was originally added here
05-12 10:05:34.465 11242 11242 E WindowManager: 	at android.view.ViewRootImpl.<init>(ViewRootImpl.java:614)
05-12 10:05:34.465 11242 11242 E WindowManager: 	at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:377)
05-12 10:05:34.465 11242 11242 E WindowManager: 	at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:95)
05-12 10:05:34.465 11242 11242 E WindowManager: 	at android.app.Dialog.show(Dialog.java:342)
05-12 10:05:34.465 11242 11242 E WindowManager: 	at androidx.appcompat.app.AlertDialog$Builder.show(AlertDialog.java:1009)
05-12 10:05:34.465 11242 11242 E WindowManager: 	at dsfingerprint.dy.txx.com.dsfingerprint.MainActivity.BtnOnClick(MainActivity.java:252)
05-12 10:05:34.465 11242 11242 E WindowManager: 	at java.lang.reflect.Method.invoke(Native Method)
05-12 10:05:34.465 11242 11242 E WindowManager: 	at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409)
05-12 10:05:34.465 11242 11242 E WindowManager: 	at android.view.View.performClick(View.java:7150)
05-12 10:05:34.465 11242 11242 E WindowManager: 	at android.view.View.performClickInternal(View.java:7123)
05-12 10:05:34.465 11242 11242 E WindowManager: 	at android.view.View.access$3500(View.java:807)
05-12 10:05:34.465 11242 11242 E WindowManager: 	at android.view.View$PerformClick.run(View.java:27540)
05-12 10:05:34.465 11242 11242 E WindowManager: 	at android.os.Handler.handleCallback(Handler.java:883)
05-12 10:05:34.465 11242 11242 E WindowManager: 	at android.os.Handler.dispatchMessage(Handler.java:100)
05-12 10:05:34.465 11242 11242 E WindowManager: 	at android.os.Looper.loop(Looper.java:214)
05-12 10:05:34.465 11242 11242 E WindowManager: 	at android.app.ActivityThread.main(ActivityThread.java:7554)
05-12 10:05:34.465 11242 11242 E WindowManager: 	at java.lang.reflect.Method.invoke(Native Method)
05-12 10:05:34.465 11242 11242 E WindowManager: 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
05-12 10:05:34.465 11242 11242 E WindowManager: 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:980)
05-12 10:05:34.467 11242 11289 D Surface : Surface::disconnect(this=0x71d37ed000,api=1)
05-12 10:05:34.469 11242 11242 D View    : [Warning] assignParent to null: this = DecorView@687d778[MainActivity]

原因是,AlertDialog需要依附于Activity而存在,此时Activity被销毁,AlertDialog却未被dismiss,导致其依赖的上下文不存在。无法回收。

05-12 10:28:25.159 13004 13004 D TAG     : onPause: alertDialog.isShowing():true
05-12 10:28:25.165 13004 13004 D TAG     : onStop: alertDialog.isShowing():true
05-12 10:28:25.169 13004 13004 D TAG     : onDestroy: alertDialog.isShowing():true
05-12 10:28:25.182 13004 13057 D Surface : Surface::disconnect(this=0x71ce760000,api=1)
05-12 10:28:25.184 13004 13004 D View    : [Warning] assignParent to null: this = DecorView@459eb07[MainActivity]
05-12 10:28:25.205 13004 13004 E WindowManager: 
05-12 10:28:25.205 13004 13004 E WindowManager: android.view.WindowLeaked: Activity dsfingerprint.dy.txx.com.dsfingerprint.MainActivity has leaked window DecorView@687d778[MainActivity] that was originally added here
05-12 10:28:25.205 13004 13004 E WindowManager: 	at android.view.ViewRootImpl.<init>(ViewRootImpl.java:614)
05-12 10:28:25.205 13004 13004 E WindowManager: 	at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:377)
05-12 10:28:25.205 13004 13004 E WindowManager: 	at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:95)
05-12 10:28:25.205 13004 13004 E WindowManager: 	at android.app.Dialog.show(Dialog.java:342)
05-12 10:28:25.205 13004 13004 E WindowManager: 	at androidx.appcompat.app.AlertDialog$Builder.show(AlertDialog.java:1009)
05-12 10:28:25.205 13004 13004 E WindowManager: 	at dsfingerprint.dy.txx.com.dsfingerprint.MainActivity.BtnOnClick(MainActivity.java:255)
05-12 10:28:25.205 13004 13004 E WindowManager: 	at java.lang.reflect.Method.invoke(Native Method)
05-12 10:28:25.205 13004 13004 E WindowManager: 	at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409)
05-12 10:28:25.205 13004 13004 E WindowManager: 	at android.view.View.performClick(View.java:7150)
05-12 10:28:25.205 13004 13004 E WindowManager: 	at android.view.View.performClickInternal(View.java:7123)
05-12 10:28:25.205 13004 13004 E WindowManager: 	at android.view.View.access$3500(View.java:807)
05-12 10:28:25.205 13004 13004 E WindowManager: 	at android.view.View$PerformClick.run(View.java:27540)
05-12 10:28:25.205 13004 13004 E WindowManager: 	at android.os.Handler.handleCallback(Handler.java:883)
05-12 10:28:25.205 13004 13004 E WindowManager: 	at android.os.Handler.dispatchMessage(Handler.java:100)
05-12 10:28:25.205 13004 13004 E WindowManager: 	at android.os.Looper.loop(Looper.java:214)
05-12 10:28:25.205 13004 13004 E WindowManager: 	at android.app.ActivityThread.main(ActivityThread.java:7554)
05-12 10:28:25.205 13004 13004 E WindowManager: 	at java.lang.reflect.Method.invoke(Native Method)
05-12 10:28:25.205 13004 13004 E WindowManager: 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
05-12 10:28:25.205 13004 13004 E WindowManager: 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:980)
05-12 10:28:25.207 13004 13057 D Surface : Surface::disconnect(this=0x71ce930000,api=1)
05-12 10:28:25.209 13004 13004 D View    : [Warning] assignParent to null: this = DecorView@687d778[MainActivity]
05-12 10:28:25.393 13004 13004 D TAG     : onCreate: 
05-12 10:28:25.408 13004 13004 D knealq  : onResume: i:1

解决办法:
在销毁Activity之前,主动将Alertdialog dismiss。

    protected void onPause() {
        super.onPause();

        if(null != alertDialog && alertDialog.isShowing()){
            Log.d("TAG", "onPause: alertdialog showing, dismiss it");
            alertDialog.dismiss();
        }
    }

再看log,不再报leak window了:

05-12 10:18:11.978 12708 12708 D TAG     : getProp: unknown
05-12 10:18:11.979 12708 12708 D TAG     : onPause: alertdialog showing, dismiss it
05-12 10:18:11.986 12708 12757 D Surface : Surface::disconnect(this=0x7170e7a000,api=1)
05-12 10:18:11.988 12708 12708 D View    : [Warning] assignParent to null: this = DecorView@58f3ef1[MainActivity]
05-12 10:18:12.001 12708 12708 D TAG     : onStop: alertDialog.isShowing():false
05-12 10:18:12.005 12708 12708 D TAG     : onDestroy: alertDialog.isShowing():false
05-12 10:18:12.015 12708 12757 D Surface : Surface::disconnect(this=0x7170e45000,api=1)
05-12 10:18:12.018 12708 12708 D View    : [Warning] assignParent to null: this = DecorView@ca42725[MainActivity]
05-12 10:18:12.167 12708 12708 D TAG     : onCreate: 
05-12 10:18:12.182 12708 12708 D knealq  : onResume: i:1
05-12 10:18:12.183 12708 12708 D TAG     : onAnimationUpdate: 0
05-12 10:18:12.184 12708 12708 D TAG     : onResume: mExpandedFraction: 0.2737995
05-12 10:18:12.184 12708 12708 D TAG     : onResume: getScreenWidth: 720
05-12 10:18:12.184 12708 12708 D TAG     : onResume: getScreenHeight: 1461
05-12 10:18:12.184 12708 12708 D TAG     : onResume: checkDeviceHasNavigationBar: true
05-12 10:18:12.185 12708 12708 D TAG     : getNavigationBarHeight: 96.0
05-12 10:18:12.185 12708 12708 D TAG     : onResume: getNavigationBarHeight: 96
05-12 10:18:12.186 12708 12708 D TAG     : onResume: getStatusBarHeight: 43
05-12 10:18:12.186 12708 12708 D TAG     : onResume: duijiao: 39.833196157492175
05-12 10:18:12.186 12708 12708 D TAG     : onResume: duijiao2: 1754.5369759569046
05-12 10:18:12.191 12708 12708 V PhoneWindow: DecorView setVisiblity: visibility = 4, Parent = null, this = DecorView@5d3fc27[]
05-12 10:18:12.196 12708 12708 D ViewRootImpl[MainActivity]: hardware acceleration = true , fakeHwAccelerated = false, sRendererDisabled = false, forceHwAccelerated = false, sSystemRendererDisabled = false
05-12 10:18:12.202 12708 12708 V PhoneWindow: DecorView setVisiblity: visibility = 0, Parent = android.view.ViewRootImpl@21d1372, this = DecorView@5d3fc27[MainActivity]
05-12 10:18:12.205 12708 12708 D TAG     : onAnimationUpdate: 0
05-12 10:18:12.241 12708 12708 E GraphicExt: GraphicExtModuleLoader::CreateGraphicExtInstance false
05-12 10:18:12.243 12708 12757 D Surface : Surface::connect(this=0x716fb2d000,api=1)
05-12 10:18:12.245 12708 12757 D Surface : Surface::setBufferCount(this=0x716fb2d000,bufferCount=3)
05-12 10:18:12.246 12708 12757 D Surface : Surface::allocateBuffers(this=0x716fb2d000)
05-12 10:18:12.282 12708 12708 D TAG     : onDismiss: androidx.appcompat.app.AlertDialog@cc2491f
05-12 10:18:12.294 12708 12708 D TAG     : onAnimationUpdate: 6
05-12 10:18:12.330 12708 12708 I AssistStructure: Flattened final assist data: 2028 bytes, containing 1 windows, 10 views
05-12 10:18:12.333 12708 12708 D TAG     : onAnimationUpdate: 12
05-12 10:18:12.360 12708 12708 D TAG     : onAnimationUpdate: 20

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值