Search code examples
androidapkads

Admobs ad not showing properly


I have created a simple board game for android and I have a problem with the ads after the game finishes. I use this simple function

public void afterEnd(){
        showInterstitial(); //dixnei tin diafimisi
        goToNextLevel();    //paei sto ending PRIN tin diafimisi gia na min fenete periergo
    }

To show the ad and then go to the next activity. The thing is the ad never shows up at that time.Game goes on to the next activity and the ad shows up only if the player presses the back key on the phone to go to the previous screen.

My 2 other functions are:

private void showInterstitial() {
        if (mInterstitialAd != null && mInterstitialAd.isLoaded()) {
            mInterstitialAd.show();
        }
    }

private void goToNextLevel() {
startActivity(new Intent(getApplicationContext(), ScoreScreen.class));
        finish();
}

If you want any other piece of code feel free to tell me.


Solution

  • There are number of events in the ad's lifecycle. You can listen for these events through the AdListener class. call goToNextLevel() method onAdClosed() method.Check this codeSnippet for better understanding.

    mInterstitialAd.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {
            // Code to be executed when an ad finishes loading.
    
        }
    
        @Override
        public void onAdFailedToLoad(int errorCode) {
            // Code to be executed when an ad request fails.
        }
    
        @Override
        public void onAdOpened() {
            // Code to be executed when the ad is displayed.
        }
    
        @Override
        public void onAdClicked() {
            // Code to be executed when the user clicks on an ad.
        }
    
        @Override
        public void onAdLeftApplication() {
            // Code to be executed when the user has left the app.
        }
    
        @Override
        public void onAdClosed() {
            // Code to be executed when the interstitial ad is closed.
             goToNextLevel()
        }
    });