App Open Ad Admob Beta Implementation and Integrating in Kotlin

Hello Guys, Google has launched new ad type for its Mobile Ads Admob. It's called App Open Ads. It's might be limited for some developers because still beta version. Lets try this ad and how it works. From Google developers guide site showed only in Java Code. Now, I have Convert it to Kotlin.

App Open Ad Admob Beta Implementation and Integrating in Kotlin
source : Google developer

What is App Open AD?

This is a new AD type that we can show to users when he minimises our app and comes back. An ad format that appears when users open or switches back to your app. Ad overlays the loading screen.

Let’s start to implement this Ad.

Step 1

Add the following dependency or update the version if you are implementing it in your old project. it should be 19.4.0 or Latest.

Step 2

Create MyApplication class and copy code below ! 

class MyApplication : Application () {

private lateinit var appOpenManager: AppOpenManager

override fun onCreate() {
MobileAds.initialize(this) {}
appOpenManager = AppOpenManager(this)


Step 3
Pass this application class to your manifest and also add your AdMob ad id in metadata as following.




Step 4 :
Create Class AppOpenManager and copy the code Below !

class AppOpenManager (private var myApplication: MyApplication) : LifecycleObserver,Application.ActivityLifecycleCallbacks {
companion object {
private const val LOG_TAG = "AppOpenManager"
private const val AD_UNIT_ID = "ca-app-pub-3940256099942544/3419835294"


private var appOpenAd: AppOpenAd? = null
private lateinit var loadCallback: AppOpenAd.AppOpenAdLoadCallback
private var currentActivity : Activity? = null
private var isShowingAd : Boolean = false
private var loadTime:Long = 0


fun onStart() {
Log.d(LOG_TAG, "onStart")

fun showAdIfAvailable() {
// Only show ad if there is not already an app open ad currently showing
// and an ad is available.
if (!isShowingAd && isAdAvailable())


Log.d(LOG_TAG, "Will show ad.")
val fullScreenContentCallback = object: FullScreenContentCallback() {
override fun onAdDismissedFullScreenContent() {
// Set the reference to null so isAdAvailable() returns false.
this@AppOpenManager.appOpenAd = null
isShowingAd = false
override fun onAdFailedToShowFullScreenContent(adError: AdError) {}
override fun onAdShowedFullScreenContent() {
isShowingAd = true
appOpenAd?.show(currentActivity, fullScreenContentCallback)
Log.d(LOG_TAG, "Can not show ad.")
/** Request an ad */
fun fetchAd() {
// We will implement this below.
if (isAdAvailable()) {
loadCallback = object:AppOpenAd.AppOpenAdLoadCallback() {
* Called when an app open ad has loaded.
* @param ad the loaded app open ad.
override fun onAppOpenAdLoaded(ad:AppOpenAd) {
this@AppOpenManager.appOpenAd = ad
this@AppOpenManager.loadTime = (Date()).getTime()
* Called when an app open ad has failed to load.
* @param loadAdError the error.
override fun onAppOpenAdFailedToLoad(loadAdError: LoadAdError) {
// Handle the error.


val request : AdRequest = getAdRequest()
myApplication, AD_UNIT_ID, request,


/** Creates and returns ad request. */
private fun getAdRequest():AdRequest {
return AdRequest.Builder().build()


/** Utility method that checks if ad exists and can be shown. */
fun isAdAvailable():Boolean {
return appOpenAd != null && wasLoadTimeLessThanNHoursAgo(4)


/** Utility method to check if ad was loaded more than n hours ago. */
private fun wasLoadTimeLessThanNHoursAgo(numHours:Long):Boolean {
val dateDifference = (Date()).time - this.loadTime
val numMilliSecondsPerHour:Long = 3600000
return (dateDifference < (numMilliSecondsPerHour * numHours))


override fun onActivityPaused(activity: Activity) {


override fun onActivityStarted(activity: Activity) {

currentActivity = activity

override fun onActivityDestroyed(activity: Activity) {
currentActivity = null

override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {

override fun onActivityStopped(activity: Activity) {


override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {


override fun onActivityResumed(activity: Activity) {

currentActivity = activity
In the above code replace AD_UNIT_ID with your Admob AD id.

Step 5

Now in the above class, you will get an error ProcessLifecycleOwner not able to resolve. 
To solve that we need to implement the following dependencies.

def lifecycle_version = "2.0.0"
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"implementation "androidx.lifecycle:lifecycle-runtime:$lifecycle_version" 
annotationProcessor "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"

All done.
Now we will run our app and see how it looks. To test it minimize your app and open it again.

If you are getting any error try to replace your AD id with test ad id. Because AdMob takes some time to serve ads on new ads ids.Hope you enjoyed the blog. 

Here is the sample source code for the implementation of Admob Open Ad.

Post a Comment for "App Open Ad Admob Beta Implementation and Integrating in Kotlin"