【问题标题】:Memory leak with ProgressDialog in android Application classandroid应用程序类中的ProgressDialog内存泄漏
【发布时间】:2018-12-30 15:22:28
【问题描述】:

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

我的申请代码是:

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

当我使用进度开/关时:

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

错误图片是:

【问题讨论】:

    标签: android memory-leaks progressdialog lottie


    【解决方案1】:

    你为什么在你的应用程序类中使用它?我认为您泄漏活动是因为您在应用程序中保存了对您的progressDialog 的引用,它在这里保存了对活动的引用。

    progressDialog = new AppCompatDialog(activity);
    

    您应该在 progressDialog.dismiss(); 之后调用 progressDialog = null 并将其从应用程序中移出到单独的类,因为您正在破坏 SOLID。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-01
      • 1970-01-01
      • 2016-11-23
      相关资源
      最近更新 更多