Repository navigation
fix(android): wait for SafeAreaView insets to mount before drawing - #815
Open
LizunovSergey wants to merge 1 commit into
Open
LizunovSergey wants to merge 1 commit into
LizunovSergey wants to merge 1 commit into
Conversation
With the New Architecture, SafeAreaView sends its insets to the shadow node through StateWrapper.updateState. Fabric applies that update asynchronously: it waits for the next event beat, React Native commits it on the JS thread, and the padding reaches the view in a later mount. onPreDraw cancelled only one draw, so the next frame drew the view without its insets. SafeAreaView now remembers the insets it sent and keeps cancelling draws until the mounted state holds them. Each wait ends after 100 ms at most, so a busy JS thread cannot stop the window from drawing. Fixes appandflow#811
|
@LizunovSergey is attempting to deploy a commit to the appandflow Team on Vercel. A member of the Team first needs to authorize it. |
floco-af
reviewed
Nov 24, 2026
floco-af
left a comment
Contributor
There was a problem hiding this comment.
Thanks for the fix and the detailed write-up. I read the diff against main and the way Fabric mounts state, and this looks correct to me.
What I checked
- The comparison is exact on both sides.
mPendingStateInsetsuses the samePixelUtil.toDIPFromPixelconversion asedgeInsetsToJsMap. The state value goes Float → Double → Float, which round-trips without loss, soEdgeInsets ==(a data class) will match once the state is mounted. - The 100 ms cap works because
ViewRootImplcallsscheduleTraversals()again after anonPreDrawcancel.onPreDrawtherefore keeps running each frame, so the timeout is reached even if nothing else invalidates the view. - The pending state is cleared on detach and when the cap is hit, so it can't leak into a re-attach.
- The non-Fabric path (
stateWrapper == null) is unchanged and still cancels a single draw. - The diff is contained to one file and has no API or breaking changes.
Notes
- If the C++ state ever stores the insets differently from what Android sends (a different rounding, say),
isWaitingForState()would never match. Every inset change would then cost a 100 ms draw hold instead of failing visibly. I couldn't find such a case in the current state definition. A debug log when the cap is hit would make it easy to catch. - The hold applies to the whole window, since the listener sits on the provider view. The PR already says so. It's worth keeping the 100 ms value in mind if a screen changes insets continuously, for example during a dock or edge-to-edge transition.
- There's no automated test. For this kind of timing problem I think a test isn't practical, so the emulator before/after table in the description has to carry the validation.
Validation: I didn't re-run the emulator measurement independently. The 8/8 → 0/8 results are the author's. I did confirm that the code path matches the mechanism described in #811.
Verdict from my side: looks good. It still needs a maintainer to review it and approve the Vercel preview (that failure is the fork authorization, not a code problem).
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
On Android with the New Architecture, a
SafeAreaViewthat mounts after startup draws its first frame without insets. The next frame moves the content into place.Under a provider, the view computes its insets in
onAttachedToWindowand sends them withStateWrapper.updateState. Fabric applies that update in the default async mode: it waits for the next event beat, React Native commits it on the JS thread, and the padding reaches the view in a later mount. The firstonPreDrawsees no change, so that frame draws without padding. The single cancelled draw inonPreDrawonly helps when the insets change later.Android has no synchronous path for this.
StateWrapper.updateStatetakes no update mode (React Native 0.85.3 and 0.88.1), so theunstable_Immediatefix from #803 does not carry over.This PR makes the view remember the insets it sent.
onPreDrawkeeps cancelling draws until the mounted state holds those insets, for 100 ms at most, so a busy JS thread cannot stop the window from drawing.StateWrapper, the view still cancels one draw.modeoredgeschange that sends the same insets again holds no draw.Trade-off: while a view waits, the whole window skips draws, as it already does for the one cancelled draw. On the emulator the page's first frame came 31 ms later (median of 8). With the JS thread busy for 300 ms after the mount, the hold ends at about 100 ms and the content draws without insets until the state mounts, as it does on
main.Fixes #811
Test Plan
Example app, release build, New Architecture, React Native 0.85.3, Pixel 8 Pro AVD on Android 16 (API 36). A temporary screen (not in the diff) swaps in a full-screen page through
useStateunder aSafeAreaProvider. Each page has a red bar at the top and a blue bar at the bottom. A script records 8 openings per page withadb shell screenrecordand counts the openings where a bar draws away from where it settles.mainSafeAreaView(3 runs)SafeAreaView mode="margin"useSafeAreaInsets(2 runs)The first frame after the page opens:
mainon the left, this PR on the right.Also checked on this PR: rotation to landscape and back,
edgesandmodechanges, the status bar hidden and shown, and a nested provider away from the edges. The library compiles withnewArchEnabled=false, andspotlessCheckpasses. Not tested: a docked keyboard (Gboard on the AVD stayed floating) and API levels below 36.