I have loaded the interstitial ads in onCreate like this:
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
if (prefManager.shouldShowInterstitialAd()) {
interstitialAd = new InterstitialAd(this);
interstitialAd.setAdUnitId(prefManager.getInterstitialAdId());
if (!interstitialAd.isLoading() && !interstitialAd.isLoaded()) {
interstitialAd.loadAd(new AdRequest.Builder().addTestDevice("882530CA8147915F79DF99860BF2F5B0").addTestDevice("66E8C013823778CF645C155B24115C9F").build());
}
}
interstitialAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
super.onAdClosed();
if (!interstitialAd.isLoaded() && !interstitialAd.isLoading()) {
interstitialAd.loadAd(new AdRequest.Builder().addTestDevice("882530CA8147915F79DF99860BF2F5B0").addTestDevice("66E8C013823778CF645C155B24115C9F").build());
if (intentToLiveTVListActivityFromTab) {
intentToLiveTVListActivityFromTab = false;
startIntentToLiveTVListActivity();
viewPager.setCurrentItem(previousTabIndex);
tabLayout.setupWithViewPager(viewPager);
} else if (intentToLiveTVListActivity) {
intentToLiveTVListActivity = false;
startIntentToLiveTVListActivity();
} else if (intentToVideoActivity) {
intentToVideoActivity = false;
startIntentToVideoActivity();
} else if (intentToCategoryActivity) {
intentToCategoryActivity = false;
startIntentToCategoryActivity(fromNavDrawer, positionForCategoryActivity);
} else if (intentToPrivacyPolicy) {
intentToPrivacyPolicy = false;
startIntentToPrivacyPolicy();
} else if (intentToWebView) {
intentToWebView = false;
startIntentToSpecialWebView();
}
}
}
@Override
public void onAdFailedToLoad(int i) {
super.onAdFailedToLoad(i);
Log.e("Failed interstitial", "Error code: " + i);
}
});
}
@Override
public void onItemClick(View view, int position) {
Commons.logSectionItemClicked(mFirebaseAnalytics, sections.get(position).getSection());
if (interstitialAd != null && interstitialAd.isLoaded()) {
interstitialAd.show();
} else {
if (response.getSections().get(position).getSection().toLowerCase().contains("live")) {
startIntentToLiveTVListActivity();
return;
} else if (response.getSections().get(position).getSection().toLowerCase().contains("video")) {
startIntentToVideoActivity();
return;
}
startIntentToCategoryActivity("false", position);
}
if (response.getSections().get(position).getSection().toLowerCase().contains("live")) {
intentToLiveTVListActivity = true;
return;
} else if (response.getSections().get(position).getSection().toLowerCase().contains("video")) {
intentToVideoActivity = true;
return;
}
intentToCategoryActivity = true;
fromNavDrawer = "false";
positionForCategoryActivity = position;
}
}
or check the attachment for the full java code
The issue is, after interstitial ad is closed, it is not calling onAdClosed immediately. It is getting a delay of 2 or 3 seconds.