android 应用程序 class 中的 ProgressDialog 内存泄漏

Memory leak with ProgressDialog in android Application class

我在 Lottie 的应用程序 class 中使用进度对话框,但是当显示进度对话框时出现内存泄漏。我该如何解决?

我的申请代码是:

public class AppController extends Application {
public static final String TAG = AppController.class.getSimpleName();
private static AppController mInstance;
AppCompatDialog progressDialog;

@Override
public void onCreate() {
    super.onCreate();
    if (LeakCanary.isInAnalyzerProcess(this)) {
        // This process is dedicated to LeakCanary for heap analysis.
        // You should not init your app in this process.
        return;
    }
    LeakCanary.install(this);
    // Normal app init code...

    mInstance = this;
}

public static synchronized AppController getInstance() {
    return mInstance;
}

/**
 * Progress Dialog
 */
public void progressON(Activity activity) {
    if (activity == null) {
        return;
    }
    if (progressDialog != null && progressDialog.isShowing()) {
    } else {
        progressDialog = new AppCompatDialog(activity);
        progressDialog.setCancelable(false);
        progressDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        progressDialog.setContentView(R.layout.progress_loading);
        progressDialog.show();
    }

    final LottieAnimationView lottieAnimationView = (LottieAnimationView) progressDialog.findViewById(R.id.progress_lottie);
    lottieAnimationView.playAnimation();

}

public void progressOFF() {
    if (progressDialog != null && progressDialog.isShowing()) {
        progressDialog.dismiss();
    }
}
}

当我使用进度时 on/off:

AppController.getInstance().progressON(PartnerDetailActivity.this);
AppController.getInstance().progressOFF();

错误图片是:

为什么要在您的应用程序中使用它 class?我认为您泄漏 activity 是因为您在应用程序中保存了对 progressDialog 的引用,这在此处保存了对 activity 的引用。

progressDialog = new AppCompatDialog(activity);

您应该在 progressDialog.dismiss(); 之后调用 progressDialog = null 并将其从应用程序中移出以分离 class,因为您正在破坏 SOLID。