- #1
Darkmisc
- 220
- 31
- TL;DR Summary
- I'd like to show an interstitial ad when a button is clicked to go from MainActivity to Activity2. Instead, the ad shows while clicking back from Activity2 to MainActivity.
Hi everyone
I'd like to show an interstitial ad while going from MainActivity to Activity2. Instead, the ad only shows when clicking back from Activity2 to the MainActivity (using the system back button. Activity2 doesn't have its own back button).
Does anyone know what I've done wrong?
I have a button in MainActivity that loads Activity2. I have the following in the code for the button.
I thought I could solve the problem by replacing MainActivity.this with Activity2, but it get a red underline. I've tried "Activity2," "Activity2.class" and "Activity2.java."
The below code is for loading and calling the ads.
Thanks
I'd like to show an interstitial ad while going from MainActivity to Activity2. Instead, the ad only shows when clicking back from Activity2 to the MainActivity (using the system back button. Activity2 doesn't have its own back button).
Does anyone know what I've done wrong?
I have a button in MainActivity that loads Activity2. I have the following in the code for the button.
show ad:
if (mInterstitialAd != null) {
mInterstitialAd.show(MainActivity.this);
} else {
Log.i(TAG, "Ad failed to load");
}
I thought I could solve the problem by replacing MainActivity.this with Activity2, but it get a red underline. I've tried "Activity2," "Activity2.class" and "Activity2.java."
The below code is for loading and calling the ads.
loading and calling ads:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(this, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
loadInterstitial();
public void loadInterstitial(){
AdRequest adRequest = new AdRequest.Builder().build();
InterstitialAd.load(this, getString(R.string.interstitial_ad_unit_id), adRequest, new InterstitialAdLoadCallback() {
@Override
public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
super.onAdLoaded(interstitialAd);
Log.d(TAG, "Ad loaded successfully");
mInterstitialAd = interstitialAd;
mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {
@Override
public void onAdFailedToShowFullScreenContent(@NonNull AdError adError) {
super.onAdFailedToShowFullScreenContent(adError);
Log.d(TAG, "Ad failed to show");
}
@Override
public void onAdShowedFullScreenContent() {
super.onAdShowedFullScreenContent();
Log.d(TAG, "Ad shown successfully");
mInterstitialAd = null;
} @Override
public void onAdDismissedFullScreenContent() {
super.onAdDismissedFullScreenContent();
Log.d(TAG, "Ad dismissed");
}
});
}
@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
super.onAdFailedToLoad(loadAdError);
Log.i(TAG, "onAdLoaded");
mInterstitialAd = null;
}
});
}