From 1ffef5669c21f4b2c5fec6bc58a85f95518cf10e Mon Sep 17 00:00:00 2001 From: Eric Rozell Date: Mon, 7 Oct 2024 16:09:53 -0700 Subject: [PATCH] Fix for multi-root apps with Modal (#46867) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/46867 In multi-root Android React Native apps (e.g., multiple ReactFragments), if the following sequence occurs: 1. a Modal is displayed via the secondary root 2. the secondary root is destroyed 3. the app is backgrounded 4. the app is foregrounded The LifecycleEventListener on the ReactModalHostView will fire, causing the Modal to be rehydrated in the onHostResume callback. Removing the lifecycle event listener when the modal is detached from the window resolves the issue. ## Changelog [Android][Fixed] Fix issues with Modals and lifecycle events in multi-surface apps Reviewed By: alanleedev Differential Revision: D64001103 fbshipit-source-id: 10f8304cd9cca0d5c90e39f5e361290f4fc35283 --- .../com/facebook/react/views/modal/ReactModalHostView.kt | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/modal/ReactModalHostView.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/modal/ReactModalHostView.kt index f61fa85d7b8..ed607aa289c 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/modal/ReactModalHostView.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/modal/ReactModalHostView.kt @@ -112,7 +112,6 @@ public class ReactModalHostView(context: ThemedReactContext) : private var createNewDialog = false init { - context.addLifecycleEventListener(this) dialogRootViewGroup = DialogRootViewGroup(context) } @@ -131,9 +130,14 @@ public class ReactModalHostView(context: ThemedReactContext) : dialogRootViewGroup.id = id } + protected override fun onAttachedToWindow() { + super.onAttachedToWindow() + (context as ThemedReactContext).addLifecycleEventListener(this) + } + protected override fun onDetachedFromWindow() { super.onDetachedFromWindow() - dismiss() + onDropInstance() } public override fun addView(child: View?, index: Int) {