Skip to content

Commit

Permalink
Fix deprecated lifecycle annotation.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 633951797
  • Loading branch information
JillSong authored and Copybara-Service committed May 15, 2024
1 parent 0feec49 commit 2a5cd11
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.view.MenuItem
import android.view.View
import android.widget.PopupMenu
import android.widget.Toast
import androidx.activity.OnBackPressedCallback
import androidx.appcompat.app.AppCompatActivity

/** The main activity in the app. */
Expand All @@ -17,6 +18,15 @@ class MainActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

// Override the default implementation when the user presses the back key.
val onBackPressedCallback: OnBackPressedCallback =
object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
moveTaskToBack(true)
}
}
onBackPressedDispatcher.addCallback(this, onBackPressedCallback)

googleMobileAdsConsentManager = GoogleMobileAdsConsentManager.getInstance(applicationContext)
}

Expand Down Expand Up @@ -51,11 +61,4 @@ class MainActivity : AppCompatActivity() {
return super.onOptionsItemSelected(item)
}
}

/** Override the default implementation when the user presses the back key. */
override fun onBackPressed() {
// Move the task containing the MainActivity to the back of the activity stack, instead of
// destroying it. Therefore, MainActivity will be shown when the user switches back to the app.
moveTaskToBack(true)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import android.content.Context
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.ProcessLifecycleOwner
import androidx.multidex.MultiDexApplication
import com.google.android.gms.ads.AdError
Expand All @@ -24,24 +23,28 @@ private const val LOG_TAG = "MyApplication"

/** Application class that initializes, loads and show ads when activities change states. */
class MyApplication :
MultiDexApplication(), Application.ActivityLifecycleCallbacks, LifecycleObserver {
MultiDexApplication(), Application.ActivityLifecycleCallbacks, DefaultLifecycleObserver {

private lateinit var appOpenAdManager: AppOpenAdManager
private var currentActivity: Activity? = null

override fun onCreate() {
super.onCreate()
override fun onCreate(owner: LifecycleOwner) {
super<DefaultLifecycleObserver>.onCreate(owner)
registerActivityLifecycleCallbacks(this)

ProcessLifecycleOwner.get().lifecycle.addObserver(this)
appOpenAdManager = AppOpenAdManager()
}

/** LifecycleObserver method that shows the app open ad when the app moves to foreground. */
@OnLifecycleEvent(Lifecycle.Event.ON_START)
fun onMoveToForeground() {
// Show the ad (if available) when the app moves to foreground.
currentActivity?.let { appOpenAdManager.showAdIfAvailable(it) }
/**
* DefaultLifecycleObserver method that shows the app open ad when the app moves to foreground.
*/
override fun onStart(owner: LifecycleOwner) {
super.onStart(owner)
currentActivity?.let {
// Show the ad (if available) when the app moves to foreground.
appOpenAdManager.showAdIfAvailable(it)
}
}

/** ActivityLifecycleCallback methods. */
Expand Down Expand Up @@ -151,7 +154,7 @@ class MyApplication :
Log.d(LOG_TAG, "onAdFailedToLoad: " + loadAdError.message)
Toast.makeText(context, "onAdFailedToLoad", Toast.LENGTH_SHORT).show()
}
}
},
)
}

Expand Down Expand Up @@ -182,7 +185,7 @@ class MyApplication :
override fun onShowAdComplete() {
// Empty because the user will go back to the activity that shows the ad.
}
}
},
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<resources>
<string name="app_name" translatable="false">AdMob App Open Example</string>
<string name="app_name" translatable="false">Ad Manager App Open Example</string>
<string name="main_activity" translatable="false">Main Activity</string>
<string name="main_activity_text" translatable="false">
Leave and switch back to the app to see the ad.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.view.MenuItem
import android.view.View
import android.widget.PopupMenu
import android.widget.Toast
import androidx.activity.OnBackPressedCallback
import androidx.appcompat.app.AppCompatActivity

/** The main activity in the app. */
Expand All @@ -17,6 +18,15 @@ class MainActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

// Override the default implementation when the user presses the back key.
val onBackPressedCallback: OnBackPressedCallback =
object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
moveTaskToBack(true)
}
}
onBackPressedDispatcher.addCallback(this, onBackPressedCallback)

googleMobileAdsConsentManager = GoogleMobileAdsConsentManager.getInstance(applicationContext)
}

Expand Down Expand Up @@ -51,11 +61,4 @@ class MainActivity : AppCompatActivity() {
return super.onOptionsItemSelected(item)
}
}

/** Override the default implementation when the user presses the back key. */
override fun onBackPressed() {
// Move the task containing the MainActivity to the back of the activity stack, instead of
// destroying it. Therefore, MainActivity will be shown when the user switches back to the app.
moveTaskToBack(true)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import android.content.Context
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.ProcessLifecycleOwner
import androidx.multidex.MultiDexApplication
import com.google.android.gms.ads.AdError
Expand All @@ -24,24 +23,28 @@ private const val LOG_TAG = "MyApplication"

/** Application class that initializes, loads and show ads when activities change states. */
class MyApplication :
MultiDexApplication(), Application.ActivityLifecycleCallbacks, LifecycleObserver {
MultiDexApplication(), Application.ActivityLifecycleCallbacks, DefaultLifecycleObserver {

private lateinit var appOpenAdManager: AppOpenAdManager
private var currentActivity: Activity? = null

override fun onCreate() {
super.onCreate()
override fun onCreate(owner: LifecycleOwner) {
super<DefaultLifecycleObserver>.onCreate(owner)
registerActivityLifecycleCallbacks(this)

ProcessLifecycleOwner.get().lifecycle.addObserver(this)
appOpenAdManager = AppOpenAdManager()
}

/** LifecycleObserver method that shows the app open ad when the app moves to foreground. */
@OnLifecycleEvent(Lifecycle.Event.ON_START)
fun onMoveToForeground() {
// Show the ad (if available) when the app moves to foreground.
currentActivity?.let { appOpenAdManager.showAdIfAvailable(it) }
/**
* DefaultLifecycleObserver method that shows the app open ad when the app moves to foreground.
*/
override fun onStart(owner: LifecycleOwner) {
super.onStart(owner)
currentActivity?.let {
// Show the ad (if available) when the app moves to foreground.
appOpenAdManager.showAdIfAvailable(it)
}
}

/** ActivityLifecycleCallback methods. */
Expand Down Expand Up @@ -151,7 +154,7 @@ class MyApplication :
Log.d(LOG_TAG, "onAdFailedToLoad: " + loadAdError.message)
Toast.makeText(context, "onAdFailedToLoad", Toast.LENGTH_SHORT).show()
}
}
},
)
}

Expand Down Expand Up @@ -182,7 +185,7 @@ class MyApplication :
override fun onShowAdComplete() {
// Empty because the user will go back to the activity that shows the ad.
}
}
},
)
}

Expand Down

0 comments on commit 2a5cd11

Please sign in to comment.