From 410bcdc7917e3ba21567020f84bcfffb3513edfc Mon Sep 17 00:00:00 2001 From: Taylor Dale <5500716+TaylorDale@users.noreply.github.com> Date: Tue, 3 Dec 2024 03:46:30 +1000 Subject: [PATCH] fix(android): use currentActivity in initialize if !null, aids use of mediation adapters (#664) * Opportunistically use currentactivity Required for Unity Ads to function, as far as I can see shouldn't effect anything else * docs: add note about maybe-dangerous use of currentActivity Hopefully no one will ever read it or care! But if there are crashes, our current understanding and reasoning is now noted for future maintainer use --------- Co-authored-by: Mike Hardy --- .../googlemobileads/ReactNativeGoogleMobileAdsModule.kt | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/android/src/main/java/io/invertase/googlemobileads/ReactNativeGoogleMobileAdsModule.kt b/android/src/main/java/io/invertase/googlemobileads/ReactNativeGoogleMobileAdsModule.kt index c3a330da..29c57164 100644 --- a/android/src/main/java/io/invertase/googlemobileads/ReactNativeGoogleMobileAdsModule.kt +++ b/android/src/main/java/io/invertase/googlemobileads/ReactNativeGoogleMobileAdsModule.kt @@ -95,7 +95,12 @@ class ReactNativeGoogleMobileAdsModule( @ReactMethod fun initialize(promise: Promise) { MobileAds.initialize( - reactApplicationContext, + // in react-native, the Activity instance *may* go away, becoming null after initialize + // it is not clear if that can happen here without an initialize necessarily following the Activity lifecycle + // it is not clear if that will cause problems even if it happens, but users that have widely deployed this + // with the use of currentActivity have not seen problems + // reference if it needs attention: https://github.com/invertase/react-native-google-mobile-ads/pull/664 + currentActivity ?: reactApplicationContext, OnInitializationCompleteListener { initializationStatus -> val result = Arguments.createArray() for ((key, value) in initializationStatus.adapterStatusMap) {