2022-06-16

Interstitial advertising (admob) only works once

These days I am doing a test, the objective is to click on a button to go to activity2 and show the interstitial advertising. For now this is the result thanks to information from: link

The problem: Upon returning to the main page (MainActivity), I pressed the button to re-enter activity2 and it is no longer displayed. Only if I close the application and open the application and press the activity button 2 the advertising is displayed.

MainActivity code

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);
        }   
     }
}

Thanks for the answers



No comments:

Post a Comment