skip to Main Content

So i recently updated my code with the latest admob SDK and dependencies.
it is supposed to show an interstitial ad before going to the next page.
upon running it the StartActivity is stuck on process dialog and wont go to the next page.
here is the code


                //Start Here
                AdRequest adRequest = new AdRequest.Builder().build();

                InterstitialAd.load(StartActivity.this,StartActivity.this.getString(R.string.main_inter), adRequest,
                        new InterstitialAdLoadCallback() {
                            @Override
                            public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
                                // The mInterstitialAd reference will be null until
                                // an ad is loaded.
                                pd.dismiss();

                                mInterstitialAd = interstitialAd;
                                mInterstitialAd.show(StartActivity.this);
                                mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback(){
                                    @Override
                                    public void onAdClicked() {
                                        // Called when a click is recorded for an ad.
                                    }

                                    @Override
                                    public void onAdDismissedFullScreenContent() {
                                        // Called when ad is dismissed.
                                        // Set the ad reference to null so you don't show the ad a second time.

                                        pd.dismiss();
                                        mInterstitialAd = null;
                                        startActivity(new Intent(StartActivity.this, MainActivity.class));
                                        StartActivity.this.finish();
                                    }

                                    @Override
                                    public void onAdFailedToShowFullScreenContent(AdError adError) {
                                        // Called when ad fails to show.
                                        mInterstitialAd = null;
                                    }

                                    @Override
                                    public void onAdImpression() {
                                        // Called when an impression is recorded for an ad.
                                    }

                                    @Override
                                    public void onAdShowedFullScreenContent() {
                                        // Called when ad is shown.
                                    }
                                });
                            }

                            @Override
                            public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
                                // Handle the error
                                mInterstitialAd = null;
                            }
                        });
            }
        });


    }
}

3

Answers


  1. You are not dismissing your progress dialog upon onAdDismissedFullScreenContent

    @Override
      public void onAdDismissedFullScreenContent() {
      // Called when ad is dismissed.
      // Set the ad reference to null so you don't show the ad a second time.
      pd.dismiss()
      mInterstitialAd = null;
      StartActivity.this.finish();
    }
    

    Always remember to handle dialog states, or it can cause window leaks.

    Login or Signup to reply.
  2. Start Activity before finish() is called

    @Override                               
        public void onAdDismissedFullScreenContent() {
            // Called when ad is dismissed.
            // Set the ad reference to null so you don't show the ad a second time.
            pd.dismiss()
            mInterstitialAd = null;
            startActivity(new Intent(StartActivity.this, NextActivity.class));
            StartActivity.this.finish();
            
    
    } 
    
    Login or Signup to reply.
  3. You should dismiss the dialog if the interstitial fails to load or show.

    
    private void openNextPage(ProgressDialog pd){
        pd.dismiss();
        mInterstitialAd = null;
        startActivity(new Intent(StartActivity.this, MainActivity.class));
        StartActivity.this.finish();
    }
    
            AdRequest adRequest = new AdRequest.Builder().build();
    
            InterstitialAd.load(StartActivity.this,
                    StartActivity.this.getString(R.string.main_inter),
                    adRequest, new InterstitialAdLoadCallback() {
                        @Override
                        public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
                            // The mInterstitialAd reference will be null until
                            // an ad is loaded.
                            // Don't dismiss dialog here as we are going to show interstitials now
    
                            mInterstitialAd = interstitialAd;
                            mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {
                                @Override
                                public void onAdClicked() {
                                    // Called when a click is recorded for an ad.
                                }
    
                                @Override
                                public void onAdDismissedFullScreenContent() {
                                    // Called when ad is dismissed.
                                    openNextPage(pd);
                                }
    
                                @Override
                                public void onAdFailedToShowFullScreenContent(AdError adError) {
                                    // Called when ad fails to show.
                                    openNextPage(pd);
                                }
    
                                @Override
                                public void onAdImpression() {
                                    // Called when an impression is recorded for an ad.
                                }
    
                                @Override
                                public void onAdShowedFullScreenContent() {
                                    // Called when ad is shown.
                                }
                            });
    
                            mInterstitialAd.show(StartActivity.this);
                        }
    
                        @Override
                        public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
                            // Handle the error
                            openNextPage(pd);
                        }
                    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search