Search code examples
androidandroid-activityadmobinterstitial

How to automatically close admob interstitial ad if users click the Android home button?


I was using the interstitial ad from Admob in my android app. The interstitialAd.show() is called at certain events of the app. Everything worked fine but one annoying use case with the ad.

If users click HOME while the ad is displaying or the ad activity (i.e. users click the ad) is active, the ad or the ad activity will be displayed after switching back to the app. I would prefer that the app is displayed (i.e. the main activity is at the top of the activity stack) since it looked confusing to the users to see the ad first.

I tried to trap all the activity callbacks (such as onStart(), onResume() ...), but apparently the app will never become foreground in this use case. Since the ad activity is started by calling interstitialAd.show(), the tricks I know on managing the activity stack does not help, either.

Does anyone know how to automatically close the ad and return the app main activity to the top?

Thanks.


Solution

  • If anyone is looking for an answer to this. If the user hits the Home button while the interstitial ad is being shown, this prevents it from showing when the user launches the app again.

    In your AndroidMainifest.xml file add: android:noHistory="true" to the AdActivity activity

    Like this:

    <activity android:name="com.google.android.gms.ads.AdActivity"
       android:noHistory="true"
     android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
    </activity>
    

    This seems to be working so far.