I implements activity with fragments. In every fragment, I implemented RecyclerView. In RecyclerView after every nth row, I attached static view from My singleton class already holding views. In onDestroy of activity I remove all views from GridLayoutManager and remove all Views from RecyclerView and clear adapter objects. After doing all, I clear static views from my singleton class too. But LeakCanary detects memory leak of activity to that particular row. How to avoid memory leaks and how to remove any views holding static context?
int adPosition = FacebookAdCenter.getInstance().getAdPosition(position);
View adView;
if (adPosition >= 0) {
adView = FacebookAdCenter.getInstance().getNativeAdViewWithAdPosition(adPosition);
} else {
adView = null;
}
if (adView == null) {
adView = FacebookAdCenter.getInstance().getNativeAdView(position);
if (adView.getParent() != null) {
((ViewGroup) adView.getParent()).removeView(adView);
}
itemView.addView(adView);
} else {
if (adView.getParent() != null) {
((ViewGroup) adView.getParent()).removeView(adView);
}
itemView.addView(adView);
}
Actually I want to cache Facebook ads to reuse them for an hour. But unfortunately I am still not successful in this. If I use static reference to store ads then there is memory leak. I do not know other ways to cache Facebook ads properly.