Skip to content

Commit 638b0ac

Browse files
WMSDK-569: getContext from ReactHost in onActivityCreated callback
1 parent aeba460 commit 638b0ac

File tree

1 file changed

+33
-10
lines changed

1 file changed

+33
-10
lines changed

android/src/main/java/com/mindboxsdk/MindboxSdkLifecycleListener.kt

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,13 @@ internal class MindboxSdkLifecycleListener private constructor(
6464
application: Application,
6565
onReady: (ReactContext) -> Unit
6666
) {
67-
val reactApplication = application.getReactApplication() ?: return
6867
val reactInstanceManager = getReactInstanceManager()
6968

7069
val wrapperListener = object : ReactInstanceManager.ReactInstanceEventListener {
7170
private val called = AtomicBoolean(false)
7271
override fun onReactContextInitialized(context: ReactContext) {
7372
if (called.compareAndSet(false, true)) {
73+
Mindbox.writeLog("[RN] ReactContext initialized (listener)", Level.INFO)
7474
onReady(context)
7575
}
7676
}
@@ -90,14 +90,19 @@ internal class MindboxSdkLifecycleListener private constructor(
9090
override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
9191
if (!isMainActivity(activity)) return
9292

93-
getReactInstanceManager()?.currentReactContext?.let {
94-
onReactContextAvailable(it, activity)
95-
Mindbox.writeLog("[RN] ReactContext already available; skipping listener registration ", Level.INFO)
96-
return
97-
}
93+
val hasConsumedReactContext = AtomicBoolean(false)
9894

9995
registerReactContextListener(application) { reactContext ->
100-
onReactContextAvailable(reactContext, activity)
96+
if (hasConsumedReactContext.compareAndSet(false, true)) {
97+
onReactContextAvailable(reactContext, activity)
98+
}
99+
}
100+
101+
getReactContext()?.let {
102+
if (hasConsumedReactContext.compareAndSet(false, true)) {
103+
onReactContextAvailable(it, activity)
104+
Mindbox.writeLog("[RN] ReactContext available (pre-existing)", Level.INFO)
105+
}
101106
}
102107
}
103108

@@ -129,9 +134,7 @@ internal class MindboxSdkLifecycleListener private constructor(
129134
override fun onActivityDestroyed(activity: Activity) {
130135
if (!isMainActivity(activity)) return
131136
subscriber.onEvent(MindboxSdkLifecycleEvent.ActivityDestroyed(activity))
132-
getReactInstanceManager()
133-
?.currentReactContext
134-
?.removeActivityEventListener(activityEventListener)
137+
getReactContext()?.removeActivityEventListener(activityEventListener)
135138
activityEventListener = null
136139
}
137140

@@ -153,6 +156,26 @@ internal class MindboxSdkLifecycleListener private constructor(
153156

154157
private fun Application.getReactApplication() = this as? ReactApplication
155158

159+
private fun getReactContext(): ReactContext? {
160+
return getReactInstanceManagerContext() ?: getReactHostContext(application)
161+
}
162+
163+
private fun getReactInstanceManagerContext(): ReactContext? {
164+
return getReactInstanceManager()?.currentReactContext
165+
}
166+
167+
private fun getReactHostContext(application: Application): ReactContext? =
168+
runCatching {
169+
// RN 0.78+ moves reactContext from reactNativeHost.reactInstanceManager to reactHost
170+
val reactApplication = application as ReactApplication
171+
val getHostMethod = reactApplication.javaClass.getMethod("getReactHost")
172+
val reactHost = getHostMethod.invoke(reactApplication)
173+
val getContextMethod = reactHost.javaClass.getMethod("getCurrentReactContext")
174+
getContextMethod.invoke(reactHost) as? ReactContext
175+
}.onFailure {
176+
Mindbox.writeLog("[RN] ReactHost currentReactContext unavailable", Level.INFO)
177+
}.getOrNull()
178+
156179
private fun addReactHostListener(
157180
application: Application,
158181
wrapperListener: ReactInstanceManager.ReactInstanceEventListener

0 commit comments

Comments
 (0)