androidmemory-leaksprogressdialoglottie

Memory leak with ProgressDialog in android Application class


I use a progress Dialog in my Application class with Lottie, but there is a memory leak when progress Dialog is showing. How can I fix it?

My Application Code is:

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

and when I am using progress on/off:

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

Error image is: memory leak image


Solution

  • Why you using this in you application class? I think you leaking activity because you saving reference to your progressDialog in Application, which saving reference to activity in here.

    progressDialog = new AppCompatDialog(activity);
    

    You should call progressDialog = null after progressDialog.dismiss(); and move this out off application to separate class because you are breaking SOLID.