I have a banner ad and its callbacks that I display but when I finish the activity LeakCanary shows a memory leak:
com.android.gms,ads.nonagon.ad.webview
Leaking: YES (View.mContext references a destroyed activity)
I destroy the adView in onDestroy but the leak still occurs. I think the problem could be from binding.adView.adListener = object : AdListener() { }. How can I set adListener to null in onDestroy?
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val adRequest = AdRequest.Builder().build()
binding.adView.loadAd(adRequest)
binding.adView.adListener = object : AdListener() {
// callbacks ...
}
}
override fun onDestroy() {
super.onDestroy()
binding.adView.destroy()
}
XML file:
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/adView"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
// ...
</com.google.android.gms.ads.AdView>