android.view.WindowLeaked: Activity has leaked win

2020-06-16 03:29发布

问题:

Working with maps i have an activity which is launched when no connection is available and uses offline maps (MapQuest). The activity runs fine, map is shown, and all overlays, markers and so on. When the user clicks on one of the markers info window another activity is launched and at this moment i get a bunch of red error messages in the log, though the app does not crash. These messages (the init is in the title) seem to talk about the ZoomButtons and touch events. As for ZoomButtons or touch events (multitouch) in the code, there are only 2 lines :

map.setBuiltInZoomControls(true);
map.setMultiTouchControls(true);

and not any dialog…

  • if i write:

    map.setBuiltInZoomControls(false); map.setMultiTouchControls(false);

the red error messages disappear but of course the user cannot zoom in or out in any way…

As the error (with the "true" parameter) occurs only when launching another activity i thought that i have to add something in on pause() ie:

onPause(){
map.setBuiltInZoomControls(false);
map.setMultiTouchControls(false);
super.OnPause();
}

---- but doing so does not change anything… Any hint??? - Thanks in advance!

回答1:

Add this to your activity:

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


回答2:

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);


回答3:

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);
    }


标签: maps osmdroid