googleads / googleads-mobile-android-examples

googleads-mobile-android
Apache License 2.0
1.67k stars 1.86k forks source link

kotlin AppOpenExample not showing ads when app is moved to foreground #688

Open pas-p opened 1 month ago

pas-p commented 1 month ago

The DefaultLifecycleObserver methods in MyApplication are not called at all. I think this can be solved moving the initialization methods (registerActivityLifecycleCallbacks(this), ProcessLifecycleOwner.get().lifecycle.addObserver(this)) from DefaultLifecycleObserver. onCreate(owner: LifecycleOwner) to Application.onCreate().

SaimaUsman0333 commented 1 month ago

I have also been facing this issue. It always calls the onAdFailedToLoad callback but when I add a delay it shows fine. I am not sure if adding a delay in showing App Open Ad allowed or would it cause "Modified Ad Behavior" policy violation. By delay I mean:

`override fun onStart(owner: LifecycleOwner) { super.onStart(owner) addDelay() }

private fun addDelay() { CoroutineScope(Dispatchers.Main).launch { delay(100) // Delay for 2 seconds (2000 milliseconds) // Code after delay println("Delay finished.") currentActivity?.let { appOpenAdManager.showAdIfAvailable(it) } }`