7

I have installed in your proetk LeackCanary library. It found a memory leak, and brought me information may be requested of it, but I can not understand it because I do not have such practices in the classroom. How to understand exactly where the error is and how to fix it? Thank you.

public final class Activity extends AppCompatActivity {

InterstitialAd mInterstitialAd;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fci);
    getWindow().setBackgroundDrawable(null);

    final ImageButton btn_pas   = (ImageButton) findViewById(R.id.btn_pas);
    Glide.with(getApplicationContext()).load(R.drawable.fci_2).placeholder(R.color.white).into(btn_pas);

    mInterstitialAd = new InterstitialAd(this);
    mInterstitialAd.setAdUnitId("ca-app-pub-...");

    requestNewInterstitial();
}

@Override
public void onBackPressed() {
    if (mInterstitialAd.isLoaded()) {
        mInterstitialAd.show();
        super.onBackPressed();
    } else {
        super.onBackPressed();
    }
    requestNewInterstitial();
}

private final void requestNewInterstitial() {
    AdRequest adRequest = new AdRequest.Builder().build();
    mInterstitialAd.loadAd(adRequest);
}

public final void pas(View view) {
    Intent intent = new Intent(Activity.this, pas.class);
    startActivity(intent);
}

}

I can not attach a screenshot so I will describe writes LeakCanary.

static hk.o
references ht.a
leaks Activity instance
Denis
  • 261
  • 3
  • 10

1 Answers1

0

You are experiencing the leak because InterstitialAd saves the reference to the activity. You should replace:

mInterstitialAd = new InterstitialAd(this);

with

mInterstitialAd = new InterstitialAd(this.getApplicationContext());

For a bit more info, see my answer on a similar question.

Daniel Zolnai
  • 16,487
  • 7
  • 59
  • 71