3

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.

1234567
  • 2,226
  • 4
  • 24
  • 69

4 Answers4

2

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()
  }
Mostafa Onaizan
  • 923
  • 1
  • 5
  • 17
1

I don't know if you solved the problem.

private val lifecycleEventObserver = LifecycleEventObserver { source, event ->
    if (event == Lifecycle.Event.ON_START) {
        showAdIfAvailable()
    }
}

And then you can add what you declared as an observer like this.

ProcessLifecycleOwner.get().lifecycle.addObserver(lifecycleEventObserver)
Mehmet Peker
  • 415
  • 5
  • 10
0
  1. add this to your build.gradle, dependencies:
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.1'
  1. Create a new class e.g. MyLifecycleObserver which implements the interface "DefaultLifecycleObserver", then override onCreate, onResume, and ...
class MyLifecycleObserver: DefaultLifecycleObserver {
    override fun onCreate(owner: LifecycleOwner) {
        super.onCreate(owner)
        Log.i("lifecycleEvents", "onCreate")
    }
    override fun onResume(owner: LifecycleOwner) {
        super.onResume(owner)
        Log.i("lifecycleEvents","onResume")
    }
}
  1. In your MainActivity, just call lifecycle.addObserver() and pass an instance of MyLifecycleObserver class.
lifecycle.addObserver(MyLifecycleObserver())
Hamzeh Kabuli
  • 66
  • 1
  • 5
0

First, you should implementation this:

dependencies {
    implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
}

Then, you should write this:

class App : Application() {
    override fun onCreate() {
        super.onCreate()

        ProcessLifecycleOwner.get().lifecycle.addObserver(lifecycleEventObserver)

    }

And last, you should write this:

var lifecycleEventObserver = LifecycleEventObserver { _, event ->
        when (event) {
            Lifecycle.Event.ON_STOP -> {
                //your code here
            }
            Lifecycle.Event.ON_START -> {
                //your code here
            }
            else -> {}
        }
    }

Detailed code implementation you can read from this answer.

Stanojkovic
  • 1,612
  • 1
  • 17
  • 24