Skip to content

Instantly share code, notes, and snippets.

@MrNtlu
Created December 13, 2022 17:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MrNtlu/71b627322c50cbe02964e86550a3a513 to your computer and use it in GitHub Desktop.
Save MrNtlu/71b627322c50cbe02964e86550a3a513 to your computer and use it in GitHub Desktop.
var mInterstitialAd: InterstitialAd? = null
fun loadInterstitial(context: Context) {
InterstitialAd.load(
context,
"ca-app-pub-3940256099942544/1033173712", //Change this with your own AdUnitID!
AdRequest.Builder().build(),
object : InterstitialAdLoadCallback() {
override fun onAdFailedToLoad(adError: LoadAdError) {
mInterstitialAd = null
}
override fun onAdLoaded(interstitialAd: InterstitialAd) {
mInterstitialAd = interstitialAd
}
}
)
}
fun showInterstitial(context: Context, onAdDismissed: () -> Unit) {
val activity = context.findActivity()
if (mInterstitialAd != null && activity != null) {
mInterstitialAd?.fullScreenContentCallback = object : FullScreenContentCallback() {
override fun onAdFailedToShowFullScreenContent(e: AdError) {
mInterstitialAd = null
}
override fun onAdDismissedFullScreenContent() {
mInterstitialAd = null
loadInterstitial(context)
onAdDismissed()
}
}
mInterstitialAd?.show(activity)
}
}
fun removeInterstitial() {
mInterstitialAd?.fullScreenContentCallback = null
mInterstitialAd = null
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment