Try initializing it in onCreate, but only call the show() method from an event, such as a button click.
You can use this code, the test ids work:
@Override
protected void onCreate(Bundle savedInstanceState) {
...
MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713"); //test id
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712"); //test ad
mInterstitialAd.loadAd(new AdRequest.Builder().build());
btn_test.setOnClickListener(view -> {
showInterstitial();
});
...
}
private void showInterstitial()
{
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
Log.d("TAG", "The interstitial wasn't loaded yet.");
}
}