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.
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top