Skip to content

Commit 3c7c3b1

Browse files
fix(android): apply legacy screen changes in the mount that made them
The legacy ScreenContainer posted its update to the UI queue, which runs after the frame that drew the Fabric mount. Layout changes from the same React commit (a sidebar hiding, a pane resizing) were drawn a frame or more before the screen they were made for appeared, and a popped screen stayed on screen beside the restored layout. Run the pending update from the Fabric mount listener's didMountItems, as the v5 StackHost already does. The posted update stays for screens attached or detached outside a mount, such as by Reanimated layout animations. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 176ff64 commit 3c7c3b1

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

‎android/src/main/java/com/swmansion/rnscreens/legacy/ScreenContainer.kt‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,17 @@ import androidx.fragment.app.FragmentManager
1313
import androidx.fragment.app.FragmentTransaction
1414
import com.facebook.react.ReactRootView
1515
import com.facebook.react.bridge.ReactContext
16+
import com.facebook.react.bridge.UIManager
17+
import com.facebook.react.bridge.UIManagerListener
18+
import com.facebook.react.common.annotations.UnstableReactNativeAPI
1619
import com.facebook.react.modules.core.ReactChoreographer
1720
import com.facebook.react.uimanager.ThemedReactContext
1821
import com.facebook.react.uimanager.UIManagerHelper
1922
import com.swmansion.rnscreens.common.FragmentProviding
23+
import com.swmansion.rnscreens.helpers.getFabricUIManagerNotNull
2024
import com.swmansion.rnscreens.legacy.events.ScreenDismissedEvent
2125

26+
@OptIn(UnstableReactNativeAPI::class)
2227
open class ScreenContainer(
2328
context: Context?,
2429
) : ViewGroup(context) {
@@ -43,6 +48,22 @@ open class ScreenContainer(
4348
}
4449
private var parentScreenWrapper: ScreenFragmentWrapper? = null
4550

51+
// Applies screen changes when the Fabric mount that made them finishes, before that frame is
52+
// drawn. Otherwise the commit's other layout changes (a sidebar hiding, a pane resizing) are
53+
// drawn a frame before the screen they were made for appears or goes away.
54+
private val mountListener =
55+
object : UIManagerListener {
56+
override fun willDispatchViewUpdates(uiManager: UIManager) = Unit
57+
58+
override fun willMountItems(uiManager: UIManager) = Unit
59+
60+
override fun didMountItems(uiManager: UIManager) = performUpdates()
61+
62+
override fun didDispatchMountItems(uiManager: UIManager) = Unit
63+
64+
override fun didScheduleMountItems(uiManager: UIManager) = Unit
65+
}
66+
4667
override fun shouldDelayChildPressedState(): Boolean = false
4768

4869
override fun onLayout(
@@ -278,6 +299,9 @@ open class ScreenContainer(
278299
override fun onAttachedToWindow() {
279300
super.onAttachedToWindow()
280301
isAttached = true
302+
UIManagerHelper
303+
.getFabricUIManagerNotNull(context as ThemedReactContext)
304+
.addUIManagerEventListener(mountListener)
281305
setupFragmentManager()
282306
}
283307

@@ -323,6 +347,9 @@ open class ScreenContainer(
323347
parentScreenWrapper?.removeChildScreenContainer(this)
324348
parentScreenWrapper = null
325349

350+
UIManagerHelper
351+
.getFabricUIManagerNotNull(context as ThemedReactContext)
352+
.removeUIManagerEventListener(mountListener)
326353
super.onDetachedFromWindow()
327354
isAttached = false
328355
// When fragment container view is detached we force all its children to be removed.
@@ -360,6 +387,7 @@ open class ScreenContainer(
360387
// and disappear before the transition completes.
361388
// The exception to this rule is `performUpdatesNow` which is triggered by actions
362389
// not connected to React view hierarchy changes, but rather internal events.
390+
// `mountListener` runs the update when the mount finishes.
363391
needsUpdate = true
364392
(context as ThemedReactContext).reactApplicationContext.runOnUiQueueThread {
365393
// We schedule the update here because LayoutAnimations of `react-native-reanimated`

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-screens",
3-
"version": "5.0.0-t3.27",
3+
"version": "5.0.0-t3.28",
44
"description": "Native navigation primitives for your React Native app.",
55
"scripts": {
66
"submodules": "git submodule update --init --recursive && (cd react-navigation && yarn && yarn build && cd ../)",

0 commit comments

Comments
 (0)