Search code examples
androidinterstitial

Preloading Interstitial ads


I am running interstitial ads in my app, but I am not sure how to preload them so they appear to the user to be instantaneous. Often it can take up to 10 seconds, and the user has already moved on to different areas of the app, it's very annoying.

This is the relevant code from the app:

private void launchInterstitial() {
    interstitialAd = new InterstitialAd(this);
    interstitialAd.setAdUnitId("ca-app-pub-xxxxxxxxxxxxxxxx");

    interstitialAd.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {
            showAdInter();
        }

        @Override
        public void onAdFailedToLoad(int errorCode) {
            String message = String.format("onAdFailedToLoad (*a)", getErrorReason(errorCode));
        }

        @Override
        public void onAdClosed() {
            if (exitApp) {
                finish();
            }
        }
    });
}

private void showAdInter() {
    if (interstitialAd.isLoaded()){
        interstitialAd.show();
    } else {
        Log.d("","Interstitial ad failed to load");
    }
}

public void loadInterstitial() {
    AdRequest adRequest = new AdRequest.Builder()
            .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
            .addTestDevice("Fxxxxxxxxxxxxxxxxxxxxx")
            .build();

    interstitialAd.loadAd(adRequest);
}

I have tried calling launchInterstitial() and loadInterstitial() when the activity launches and then call showAdInter() when I want the ad to appear, but it doesn't work that way.

Anyone have any ideas?


Solution

  • Can't believe no one known anything about this, or is unwilling to help. I will post the answer here so hopefully this will actually help someone in need. I call the following method in onCreate():

    requestNewInterstitial();
    

    Method is as follows:

    private void requestNewInterstitial() {
        AdRequest adRequest = new AdRequest.Builder()
                .build();
    
        mInterstitialAd.loadAd(adRequest);
    }
    

    I create an adlistener to reload the ad again in the background after one has been closed:

    mInterstitialAd.setAdListener(new AdListener() {
            @Override
            public void onAdClosed() {
                requestNewInterstitial();
            }
        });
    

    And I call requestNewInterstitial(); on my onBackPressed() method.

    This produces smooth results, interstitials appear instantaneously.