Why does my Admob interstitial ad show at the wrong time?

  • Thread starter Thread starter Darkmisc
  • Start date Start date
  • Tags Tags
    Time
AI Thread Summary
The discussion revolves around the implementation of an interstitial ad in an Android application, specifically when transitioning from MainActivity to Activity2. The user initially faced an issue where the ad only displayed when navigating back to MainActivity from Activity2, rather than during the transition to Activity2. The code snippet provided shows the logic for loading and displaying the ad, with a focus on checking if the ad is loaded before attempting to show it. The user experimented with different context references for displaying the ad but encountered errors. Ultimately, the user resolved the issue without further elaboration, indicating that the ad now functions as intended.
Darkmisc
Messages
222
Reaction score
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.
[CODE lang="java" title="show ad"]if (mInterstitialAd != null) {
mInterstitialAd.show(MainActivity.this);
} else {
Log.i(TAG, "Ad failed to load");

}[/CODE]

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.

[CODE lang="java" title="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;
}
});

}[/CODE]Thanks
 
Technology news on Phys.org
nvm. It works now.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top