Skip to content

Commit 00e4d01

Browse files
Nicholas Ventimigliacopybara-github
authored andcommitted
Updated AppOpen kotlin sample to use ad preloading.
PiperOrigin-RevId: 797940528
1 parent c5ebdd0 commit 00e4d01

6 files changed

Lines changed: 137 additions & 200 deletions

File tree

kotlin/NextGenExample/app/src/main/java/com/example/nextgenexample/MyApplication.kt

Lines changed: 2 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -16,71 +16,13 @@
1616

1717
package com.example.nextgenexample
1818

19-
import android.app.Activity
2019
import android.app.Application
21-
import android.os.Bundle
22-
import androidx.fragment.app.FragmentActivity
23-
import androidx.lifecycle.DefaultLifecycleObserver
24-
import androidx.lifecycle.LifecycleOwner
25-
import androidx.lifecycle.ProcessLifecycleOwner
26-
import androidx.navigation.fragment.NavHostFragment
27-
import androidx.preference.PreferenceManager
2820
import com.example.nextgenexample.appopen.AppOpenAdManager
29-
import com.example.nextgenexample.appopen.AppOpenFragment
3021

31-
/** Application class that initializes, loads and show ads when activities change states. */
32-
class MyApplication :
33-
Application(), Application.ActivityLifecycleCallbacks, DefaultLifecycleObserver {
34-
35-
private var currentActivity: Activity? = null
22+
class MyApplication : Application() {
3623

3724
override fun onCreate() {
3825
super<Application>.onCreate()
39-
registerActivityLifecycleCallbacks(this)
40-
41-
ProcessLifecycleOwner.get().lifecycle.addObserver(this)
42-
}
43-
44-
/**
45-
* DefaultLifecycleObserver method that shows the app open ad when the app moves to foreground.
46-
*/
47-
override fun onStart(owner: LifecycleOwner) {
48-
currentActivity?.let { activity ->
49-
// Show app open ad if the switch to enable app open ads on all starts is on, or if returning
50-
// to the AppOpenFragment.
51-
52-
val isAppOpenFragment =
53-
((activity as? FragmentActivity)
54-
?.supportFragmentManager
55-
?.findFragmentById(R.id.nav_host_fragment_content_main) as? NavHostFragment)
56-
?.childFragmentManager
57-
?.primaryNavigationFragment is AppOpenFragment
58-
59-
if (
60-
PreferenceManager.getDefaultSharedPreferences(activity)
61-
.getBoolean(AppOpenFragment.KEY_SHOW_APP_OPEN_AD_ON_ALL_STARTS, false) ||
62-
isAppOpenFragment
63-
) {
64-
AppOpenAdManager.showAdIfAvailable(activity, null)
65-
return
66-
}
67-
}
68-
}
69-
70-
/** ActivityLifecycleCallback methods. */
71-
override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {}
72-
73-
override fun onActivityStarted(activity: Activity) {
74-
currentActivity = activity
26+
AppOpenAdManager.initialize(this)
7527
}
76-
77-
override fun onActivityResumed(activity: Activity) {}
78-
79-
override fun onActivityPaused(activity: Activity) {}
80-
81-
override fun onActivityStopped(activity: Activity) {}
82-
83-
override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {}
84-
85-
override fun onActivityDestroyed(activity: Activity) {}
8628
}

kotlin/NextGenExample/app/src/main/java/com/example/nextgenexample/SplashActivity.kt

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2024 Google LLC
2+
* Copyright 2026 Google LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,9 +22,7 @@ import android.os.CountDownTimer
2222
import android.util.Log
2323
import android.widget.TextView
2424
import androidx.appcompat.app.AppCompatActivity
25-
import androidx.preference.PreferenceManager
2625
import com.example.nextgenexample.appopen.AppOpenAdManager
27-
import com.example.nextgenexample.appopen.AppOpenFragment
2826
import com.example.nextgenexample.databinding.ActivitySplashBinding
2927
import com.google.android.libraries.ads.mobile.sdk.MobileAds
3028
import com.google.android.libraries.ads.mobile.sdk.common.RequestConfiguration
@@ -79,12 +77,9 @@ class SplashActivity : AppCompatActivity() {
7977

8078
/** Create the countdown timer, which counts down to zero and shows the app open ad. */
8179
private fun createTimer() {
82-
val sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this@SplashActivity)
83-
// Do not show the app open ad by default.
84-
val shouldShowAppOpenAd =
85-
sharedPrefs.getBoolean(AppOpenFragment.KEY_SHOW_APP_OPEN_AD_ON_ALL_STARTS, false)
86-
// For better usability of this sample, shorten the splash screen timer if an app open ad will
87-
// be shown.
80+
81+
val shouldShowAppOpenAd = AppOpenAdManager.isAppOpenAdOnColdStartEnabled(this)
82+
8883
val timerDurationInSeconds = if (shouldShowAppOpenAd) 5L else 2L
8984

9085
counterTextView = binding.timer
@@ -149,8 +144,8 @@ class SplashActivity : AppCompatActivity() {
149144
)
150145

151146
if (googleMobileAdsConsentManager.canRequestAds) {
152-
// Load an app open ad when Mobile Ads SDK initialization is complete.
153-
AppOpenAdManager.loadAd(this@SplashActivity)
147+
// Start preloading an app open ad when Mobile Ads SDK initialization is complete.
148+
AppOpenAdManager.startPreloading()
154149
}
155150
}
156151
}

0 commit comments

Comments
 (0)