skip to Main Content

I have some doubts about whether it is correct how the Admob interstitial code is implemented in my application.

The objective is to show interstitial ad when pressing the button that is in MainActivity to open Activity 2.

Example MainActivity (button)

public void page1(View view)  
{

    Intent i = new Intent (this, activity2.class);
    startActivity(i);

 
    if (mInterstitialAd != null ) {
        mInterstitialAd.show(this);
    }

}

The code works fine, but would it be correct?.


Another question related to the show(…):
I have seen these 2 versions

mInterstitialAd.show(this);
or
mInterstitialAd.show(MainActivity.this);

which would be the best?

Sorry I just started and I’m a bit lost.

Thanks for the answers.

2

Answers


  1. Chosen as BEST ANSWER

    After several tests I think that the two versions are correct but in the end I will use this code. (play-services-ads:21.0.0)

    How do you see it? Is the implementation correct?

    MainActivity.java:

    public class MainActivity extends Activity {
    
    
        private InterstitialAd mInterstitialAd;
        private static final String TAG = "InfoApp";
        
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main_activity);
    
    
         //***** Initialize the Mobile Ads SDK.*********** -//
            MobileAds.initialize(this, new OnInitializationCompleteListener() {
                @Override
                public void onInitializationComplete(InitializationStatus initializationStatus) {
                }
            });
            //**********************************************************//
    
          adrequest_interstitial = new AdRequest.Builder().build();
    
          InterstitialAd.load(this,ConfigPubli.Ads_interstitial, adrequest_interstitial,
                    new InterstitialAdLoadCallback()
                    {
                        @Override
                        public void onAdLoaded(@NonNull InterstitialAd interstitialAd)
                        {
                            mInterstitialAd = interstitialAd;
                            Log.i(TAG, "onAdLoaded");
    
                        }
    
                        @Override
                        public void onAdFailedToLoad(@NonNull LoadAdError loadAdError)
                        {
                            // Handle the error
                            Log.i(TAG, loadAdError.getMessage());
                            mInterstitialAd = null;
                        }
    
                    });
         }
    
    
          public void adstot(Intent i){
    
    
            mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {
                @Override
                public void onAdDismissedFullScreenContent() {
                    mInterstitialAd = null;
                    startActivity(i);
                    
                }
    
                @Override
                public void onAdFailedToShowFullScreenContent(@NonNull AdError adError) {
                    mInterstitialAd = null;
                    startActivity(i);
    
                }
    
            });
        }
    
    
    
        public void page2(View view){ //button
    
            Intent i = new Intent (this, Activity2.class);
            i.putExtra("valor","page2");
    
            int randomAd = (int)(Math.random()*10);
            if (mInterstitialAd != null && randomAd<=1) {   
                adstot(i);
                mInterstitialAd.show(this);   
            }
            else{
                startActivity(i);
            }   
         }
    }
    

    Thank you for your answers ;-)


  2. As per Interstitial Ad implementation guide,

    You should not show interstitial ads after the activity has loaded. Interstitial ad should be shown at a natural transition point. You should not show ads after page/activity is loaded. You must first show the ad and start the activity when the ad is dismissed/failed_to_show.

    Disallowed process

    enter image description here

    Allowed process

    enter image description here

    • Images from the link to make the process easier to understand

    Here’s an example:

    public void openNewActivity()  
    {
        // Create the Intent
        Intent i = new Intent (this, activity2.class);
    
        // Check mInterstitialAd 
        if (mInterstitialAd != null ) {
            // Create callback 
            mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {
                @Override
                public void onAdDismissedFullScreenContent() {
                    // Don't forget to make mIntersttialAd null
                    mInterstitialAd = null;
    
                    // times to start the activity
                    startActivity(i);
                }
    
                @Override
                public void onAdFailedToShowFullScreenContent(@NonNull AdError adError) {
                    // Don't forget to make mIntersttialAd null
                    mInterstitialAd = null;
    
                    // times to start the activity
                    startActivity(i);
                }
    
            });
            //  Now show the ad
            mInterstitialAd.show(MainActivity.this);
        } else {
            // we don't have an interstitial ad to show
            // start the activity
            startActivity(i);
        }
    }
    

    Hope this will help.

    Please note that AdMob has not forwared any code in any of its documents as a sample implementation guide which would be perfect according to its policy. But the code I’ve provided above, seems be a bit like the guidance given in the above link.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search