- It is necessary to track that the user opened the application, and not (for example) turned. This can be done by checking
if(savedInstanceState==null) is an argument in the onCreate method. After its rotation (re-creation), this argument is no longer null and the condition will not be fulfilled. - In this condition, create a request to
AdMob Launch it and wait until the advertisement is displayed.
public class MainActivity extends ActionBarActivity { boolean adsAlredyShown = false; InterstitialAd mInterstitialAd; @Override protected void onCreate(Bundle savedInstanceState) { Log.i("LOG", "onCreate"); super.onCreate(savedInstanceState); mInterstitialAd = new InterstitialAd(this); mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712"); //идентификатор из доков надо заменить на свой mInterstitialAd.setAdListener(new AdListener() { @Override public void onAdClosed() { Log.i("LOG", "onAdClosed"); } public void onAdLeftApplication() { Log.i("LOG", "onAdLeftApplication"); } @Override public void onAdLoaded() { Log.i("LOG", "onAdLoaded"); mInterstitialAd.show(); } public void onAdFailedToLoad(int errorCode) { Log.e("LOG", "onAdFailedToLoad with errorCode " + errorCode); } @Override public void onAdOpened() { Log.i("LOG", "onAdOpened"); //вызывается в момент отображения рекламы. //и раз она отобразилась ставим флаг в true //чтобы больше её не показывать adsAlredyShown = true; } }); if(savedInstanceState==null) { AdRequest adRequest = new AdRequest.Builder().build(); mInterstitialAd.loadAd(adRequest); } else { adsAlreadyShown = savedInstanceState.getBoolean("AdsAlreadyShown", false); //реклама ещё не показывалась if(!adsAlreadyShown) { AdRequest adRequest = new AdRequest.Builder().build(); mInterstitialAd.loadAd(adRequest); } } } protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putBoolean("AdsAlreadyShown", adsAlreadyShown); } }
Also, in the same savedInstanceState you can put a flag after the advertisement is shown and not show it while it is true . This flag will disappear only if the activation is killed by the system or manually using the finish() method.