Android SDK - Interstitial ads
Interstitial ads are full-screen ads that cover your app's interface until the user closes them. Show them at natural break points, such as between levels or after the user finishes a task, so they don't interrupt the experience.
Before you start, install and initialize the SDK.
1. Get the interstitial ad object
Get the ad object for your ad unit ID. The SDK caches one instance per ad unit, so calling getInterstitialAd again with the same ID returns the same object. Call after initializing MDN11 SDK.
2. Set the listener
Set the listener before calling load(), so you don't miss any events. When an ad closes or fails to show, load the next one. When loading fails, we recommend retrying with exponential backoff. See the full example.
interstitial.listener = object : InterstitialAdListener {
override fun onInterstitialAdLoaded(adInfo: AdInfo) {}
override fun onInterstitialAdLoadFailed(error: LoadError) {}
override fun onInterstitialAdShown(adInfo: AdInfo) {}
override fun onInterstitialAdShowFailed(adInfo: AdInfo?, error: ShowError) {}
override fun onInterstitialAdClicked(adInfo: AdInfo) {}
override fun onInterstitialAdClosed(adInfo: AdInfo) {}
override fun onInterstitialAdRevenue(adInfo: AdInfo) {}
}
3. Load the ad
Load the ad well before you need it, so it's ready at the next break point.
4. Show the ad
Call show() at a natural break point, once onInterstitialAdLoaded has fired.
Full example
class MainActivity : AppCompatActivity() {
private val interstitial by lazy { A11.getInterstitialAd("adUnitId") }
private val retryHandler = Handler(Looper.getMainLooper())
private var retryDelayMs = 5_000L
private var isInterstitialReady = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
interstitial.listener = object : InterstitialAdListener {
override fun onInterstitialAdLoaded(adInfo: AdInfo) {
isInterstitialReady = true
retryDelayMs = 5_000L
}
override fun onInterstitialAdLoadFailed(error: LoadError) {
Log.e("Ads", "Interstitial load failed: $error")
// Retry with exponential backoff: 5 s, 10 s, 20 s and so on, up to 60 s
retryHandler.postDelayed({ interstitial.load(this@MainActivity) }, retryDelayMs)
retryDelayMs = (retryDelayMs * 2).coerceAtMost(60_000L)
}
override fun onInterstitialAdShown(adInfo: AdInfo) {}
override fun onInterstitialAdShowFailed(adInfo: AdInfo?, error: ShowError) {
Log.e("Ads", "Interstitial show failed: $error")
interstitial.load(this@MainActivity)
}
override fun onInterstitialAdClicked(adInfo: AdInfo) {}
override fun onInterstitialAdClosed(adInfo: AdInfo) {
// Resume your app and preload the next one
interstitial.load(this@MainActivity)
}
override fun onInterstitialAdRevenue(adInfo: AdInfo) {}
}
interstitial.load(this)
}
// Call at a natural break point, for example between levels
fun showInterstitial() {
if (!isInterstitialReady) return
isInterstitialReady = false
interstitial.show(this)
}
override fun onDestroy() {
super.onDestroy()
retryHandler.removeCallbacksAndMessages(null) // Cancel pending retries
interstitial.listener = null // Release this Activity
}
}