Skip to content

fix(android): wait for SafeAreaView insets to mount before drawing - #815

Open
LizunovSergey wants to merge 1 commit into
appandflow:mainfrom
LizunovSergey:fix/811-safe-area-view-first-frame
Open

LizunovSergey wants to merge 1 commit into
appandflow:mainfrom
LizunovSergey:fix/811-safe-area-view-first-frame

Conversation

@LizunovSergey

Copy link
Copy Markdown

Summary

On Android with the New Architecture, a SafeAreaView that 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 onAttachedToWindow and sends them with StateWrapper.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 first onPreDraw sees no change, so that frame draws without padding. The single cancelled draw in onPreDraw only helps when the insets change later.

Android has no synchronous path for this. StateWrapper.updateState takes no update mode (React Native 0.85.3 and 0.88.1), so the unstable_Immediate fix from #803 does not carry over.

This PR makes the view remember the insets it sent. onPreDraw keeps 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.

  • Only the Fabric path changes. Without a StateWrapper, the view still cancels one draw.
  • A mode or edges change that sends the same insets again holds no draw.
  • The wait ends when the view detaches.

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 useState under a SafeAreaProvider. Each page has a red bar at the top and a blue bar at the bottom. A script records 8 openings per page with adb shell screenrecord and counts the openings where a bar draws away from where it settles.

Page main this PR
SafeAreaView (3 runs) 8/8, 8/8, 8/8 0/8, 0/8, 0/8
SafeAreaView mode="margin" 8/8 0/8
useSafeAreaInsets (2 runs) 0/8, 0/8 0/8, 0/8

The first frame after the page opens: main on the left, this PR on the right.

Also checked on this PR: rotation to landscape and back, edges and mode changes, the status bar hidden and shown, and a nested provider away from the edges. The library compiles with newArchEnabled=false, and spotlessCheck passes. Not tested: a docked keyboard (Gboard on the AVD stayed floating) and API levels below 36.

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
@vercel

vercel Bot commented Nov 24, 2026

Copy link
Copy Markdown

@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 floco-af left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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. mPendingStateInsets uses the same PixelUtil.toDIPFromPixel conversion as edgeInsetsToJsMap. The state value goes Float → Double → Float, which round-trips without loss, so EdgeInsets == (a data class) will match once the state is mounted.
  • The 100 ms cap works because ViewRootImpl calls scheduleTraversals() again after an onPreDraw cancel. onPreDraw therefore 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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Android] A newly mounted SafeAreaView draws its first frame without insets, then jumps (New Architecture, no navigation library)

2 participants