android.view.WindowLeaked: Activity has leaked window android.widget.ZoomButtonsController$Container that was originally added here

守給你的承諾、 提交于 2019-12-03 12:50:26

Add this to your activity:

@Override
public void finish() {
    ViewGroup view = (ViewGroup) getWindow().getDecorView();
    view.removeAllViews();
    super.finish();
}

I was having the exact same problem... thanks to you pointing out that it is caused by (likely) when the zoom controls are still visible. I tested it, and that was correct. When I pressed the back button with the zoom controls showing, it would show that leak error, if I waited until the controls faded away (they do after you stop scrolling), then there was no leak error.

A little research in WebSettings provided a method that doesn't show the zoom controls, which means it doesn't leak at anytime you want to press the back button. It does still zoom with the pinch effect though. The only disadvantage to using this method is that your controls won't show. But to me, that's worth it, since most users know about pinch zoom for all apps.

Here is what I used:

// make sure your pinch zoom is enabled
 webView.getSettings().setBuiltInZoomControls(true);

// don't show the zoom controls
 webView.getSettings().setDisplayZoomControls(false);

The problem with this leak window does not appear in the following version of Android 3.0 , so,you can try to do :

    // enabled zoom support
    getSettings().setSupportZoom(true);
    getSettings().setBuiltInZoomControls(true);

    // but,We should hide this button, otherwise in the version
    // of Android 3 and above, there is a window leak.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        // call requies API Level 11 ( Android 3.0 + )
        getSettings().setDisplayZoomControls(false);
    }
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!