Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

App Open Ad Admob @OnLifecycleEvent(ON_START) deprecated

Tags:

admob

this is the example We are following

https://developers.google.com/admob/android/app-open

In App Open Ad Admob @OnLifecycleEvent(ON_START) is used however it is deprecated . what is the alternative that we can use and how to use it.

like image 578
1234567 Avatar asked Sep 11 '25 08:09

1234567


1 Answers

delete this code

  /** 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)
//    }
//  }

and write

  private lateinit var diff: DefaultLifecycleObserver

  override fun onCreate() {
    super.onCreate()
    registerActivityLifecycleCallbacks(this)
    MobileAds.initialize(this) {}
    diff = object : DefaultLifecycleObserver {
      override fun onStart(owner: LifecycleOwner) {
        super.onStart(owner)
        currentActivity?.let {
          appOpenAdManager.showAdIfAvailable(it)
        }
      }
    }
    ProcessLifecycleOwner.get().lifecycle.addObserver(diff)
    appOpenAdManager = AppOpenAdManager()
  }
like image 83
Mostafa Onaizan Avatar answered Sep 13 '25 02:09

Mostafa Onaizan