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

  • Thread starter Darkmisc
  • Start date
  • Tags
    Time
In summary, the conversation is about an issue with showing an interstitial ad while navigating from MainActivity to Activity2. The ad only shows when using the system back button from Activity2 to MainActivity. The individual has tried replacing MainActivity.this with Activity2 in the code but it does not work. The code for loading and calling the ads is also provided. The issue is eventually resolved.
  • #1
Darkmisc
204
27
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.
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;
            }
        });

        }
Thanks
 
Technology news on Phys.org
  • #2
nvm. It works now.
 

1. Why is my Admob interstitial ad showing in the middle of gameplay or content?

This could be due to a number of factors, such as a delay in loading the ad, a glitch in the ad placement code, or an error in the ad scheduling. It is important to check your code and make sure the ad is being loaded and displayed at the appropriate time.

2. Can I control when my Admob interstitial ad appears?

Yes, you can control when your Admob interstitial ad appears by specifying the desired time or event in your ad placement code. This will ensure that the ad is shown at the appropriate time, such as between levels or after a certain amount of time has passed.

3. Why does my Admob interstitial ad show immediately after launching the app?

This could be due to a setting in your ad placement code that specifies the ad to be shown on app launch. Make sure to check your code and adjust the settings accordingly to control when the ad appears.

4. Is there a way to prevent my Admob interstitial ad from appearing at certain times?

Yes, you can use ad filters in your ad placement code to specify certain times or events in which the ad should not be shown. This can help prevent the ad from appearing at inappropriate times and improve the user experience.

5. What can I do if my Admob interstitial ad consistently shows at the wrong time?

If your Admob interstitial ad consistently shows at the wrong time, it is important to review your ad placement code and make sure it is properly configured. You may also want to reach out to the Admob support team for further assistance in troubleshooting the issue.

Similar threads

  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
11
Views
2K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
6
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
Back
Top