You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用指定依赖开发Google Maps功能时遇Activity窗口泄漏错误

Fixing the "Activity has leaked window DecorView" Error in Your Google Maps POI Feature

Hey there, let’s work through this window leak issue you’re hitting with your Google Maps app that loads nearby hospitals, restaurants, and schools. I’ve seen this exact problem a lot with location-based apps, so let’s break down what’s happening and how to fix it.

What’s Causing This?

This error means your Activity has been destroyed (like when the user presses back, or the app rotates), but some view/dialog is still attached to it—your WindowManager is still holding a reference to the Activity, which causes the memory leak. Given your setup with Google Maps and Location services, here are the most likely culprits:

  • A background location update or Places API request is still running after the Activity is destroyed, and tries to update the UI or show a dialog.
  • You’re using a dialog (like a ProgressDialog for loading POIs) that wasn’t properly dismissed when the Activity shut down.
  • Your outdated Play Services/Gradle versions have known memory leak bugs (15.0.0 is pretty old, dating back to 2018!).

Step-by-Step Fixes

1. Clean Up Lifecycle-Bound Resources

Any location listeners, API requests, or async tasks tied to your Activity need to be canceled when the Activity is no longer active.

  • For FusedLocationProviderClient, make sure to remove location updates in onStop() or onDestroy():
    private FusedLocationProviderClient fusedLocationClient;
    private LocationCallback locationCallback;
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (fusedLocationClient != null && locationCallback != null) {
            fusedLocationClient.removeLocationUpdates(locationCallback);
        }
    }
    
  • If you’re using async tasks (like fetching POIs via Places API), cancel them when the Activity is finishing. For example, with Retrofit, call call.cancel(); with AsyncTask, use task.cancel(true).

2. Properly Manage Dialogs/Views

If you’re showing a loading dialog while fetching POIs, it’s easy to forget to dismiss it when the Activity dies. Add this to your Activity’s onDestroy():

private ProgressDialog loadingDialog;

@Override
protected void onDestroy() {
    super.onDestroy();
    // Dismiss any active dialogs to prevent window leaks
    if (loadingDialog != null && loadingDialog.isShowing()) {
        loadingDialog.dismiss();
        loadingDialog = null; // Clear reference to avoid further leaks
    }
}

Also, before showing any dialog or updating the UI, always check if the Activity is still alive:

if (!isFinishing() && !isDestroyed()) {
    // Show dialog or update UI here
}

3. Outdated Dependencies? Upgrade Them

Your Play Services versions (15.0.0) and Gradle plugin (3.0.0) are quite old, and they have known memory leak issues that have been fixed in newer releases. Here’s what to update:

  • Update your Play Services dependencies in build.gradle (Module level):
    implementation 'com.google.android.gms:play-services-maps:17.0.1'
    implementation 'com.google.android.gms:play-services-location:18.0.0'
    
  • Upgrade your Gradle plugin in the Project level build.gradle:
    dependencies {
        classpath 'com.android.tools.build:gradle:7.0.4' // Match your Android Studio version
    }
    
  • Don’t forget to update your Gradle wrapper version (in gradle/wrapper/gradle-wrapper.properties) to match the plugin, e.g.:
    distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-all.zip
    

After updating, run Clean Project and Rebuild Project to clear any cached issues.

4. Debug with LeakCanary (Optional but Helpful)

If you’re still stuck, add LeakCanary to your project—it’ll automatically detect and report memory leaks with detailed stack traces, so you can see exactly which object is holding onto your Activity. Add this to your Module build.gradle:

debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.10'

Final Notes

Start with the lifecycle cleanup and dialog management first—those are the most common fixes for this error. If that doesn’t work, upgrading your dependencies should resolve any underlying bugs from the old versions.

内容的提问来源于stack exchange,提问作者Ajay Kulkarni

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:44:32