I have an activity with around four fragments and these fragments has child fragments. I have a static counter with static InterstitialAd in The Mainactivity that is called through fragments, however it leads to memory leak. Is there a better practise?
Inside main
static int clicksCount,newsCount;
private static InterstitialAd mInterstitialAd;
public static void interstitalAd() {
clicksCount++;
if (clicksCount % 3 == 0) {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
}
}
}
And whenever I need to call it from fragments
MainActivity.interstitalAd();
It's probably not necessary or wise to story the InterstitialAd
itself as a static member, since it will then retain the context it was created with indefinitely.
Better to do:
static int clicksCount,newsCount;
private InterstitialAd mInterstitialAd;
public void interstitalAd() {
clicksCount++;
if (clicksCount % 3 == 0) {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
}
}
}
And then in your Fragment
:
MainActivity mainActivity = (MainActivity)getActivity();
mainActivity.interstitialAd();