Skip to content

fix(Android, Stack v5): set needsCustomLayoutForChildren to true for StackHost#4200

Merged
kligarski merged 1 commit into
mainfrom
@kligarski/stack-v5-android-fix-orientation
Jun 24, 2026
Merged

fix(Android, Stack v5): set needsCustomLayoutForChildren to true for StackHost#4200
kligarski merged 1 commit into
mainfrom
@kligarski/stack-v5-android-fix-orientation

Conversation

@kligarski

@kligarski kligarski commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

Description

Disables layout from Yoga for children of StackHost. Fixes a nondeterministic bug with incorrect screen size on orientation change.

Closes https://github.com/software-mansion/react-native-screens-labs/issues/1499.

Details

On orientation change, sometimes measure & layout from Yoga was applied with incorrect measurements. This is very likely connected to https://github.com/software-mansion/react-native-screens-labs/discussions/543, #3295 but I didn't investigate further why we receive the outdated measurements.

In order to mitigate this, I decided to use similar strategy to StackHeaderConfig. By enabling needsCustomLayoutForChildren in StackHostViewManager, we will still receive measure with incorrect width & height from Yoga but layout won't be called. This way, incorrect measurements won't be applied and next native layout will remeasure & apply correct layout. Ultimately, we want the screen to be laid out fully by native layout, not by Yoga, so this is the desired behavior. If anything changes, StackHost receives new measurement from Yoga and will trigger native layout pass for StackScreen.

Changes

  • set needsCustomLayoutForChildren=true in StackHostViewManager

Before & after - visual documentation

Before After
before_orientation.mp4
after_orientation.mp4

Test plan

Run test-stack-toolbar-menu-show-as-action. Change orientation portrait -> landscape -> portrait multiple times. The content of the screen should be laid out correctly.

Checklist

  • Included code example that can be used to test this change.
  • For visual changes, included screenshots / GIFs / recordings documenting the change.
  • Ensured that CI passes

Copilot AI 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.

Pull request overview

This PR mitigates a nondeterministic Android orientation-change sizing issue in Stack v5 by preventing Yoga from directly laying out StackHost children, ensuring child positioning is driven by native layout.

Changes:

  • Override needsCustomLayoutForChildren() in StackHostViewManager to return true.
  • Add rationale in KDoc explaining why Yoga layout for children should be bypassed for StackHost.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@kkafar kkafar left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Makes perfect sense to me.

Base automatically changed from @kligarski/stack-v5-android-refactor to main June 24, 2026 09:53
@kligarski kligarski force-pushed the @kligarski/stack-v5-android-fix-orientation branch from d43a3f6 to b6c024b Compare June 24, 2026 09:58
@kligarski kligarski force-pushed the @kligarski/stack-v5-android-fix-orientation branch from b6c024b to c0451de Compare June 24, 2026 10:05
@kligarski kligarski merged commit 79bbe6d into main Jun 24, 2026
5 checks passed
@kligarski kligarski deleted the @kligarski/stack-v5-android-fix-orientation branch June 24, 2026 11:53
sgaczol pushed a commit that referenced this pull request Jun 30, 2026
…for `StackHost` (#4200)

## Description

Disables layout from Yoga for children of `StackHost`. Fixes a
nondeterministic bug with incorrect screen size on orientation change.

Closes
software-mansion/react-native-screens-labs#1499.

### Details

On orientation change, sometimes measure & layout from Yoga was applied
with incorrect measurements. This is very likely connected to
software-mansion/react-native-screens-labs#543,
#3295 but I
didn't investigate further why we receive the outdated measurements.

In order to mitigate this, I decided to use similar strategy to
`StackHeaderConfig`. By enabling `needsCustomLayoutForChildren` in
`StackHostViewManager`, we will still receive `measure` with incorrect
width & height from Yoga but `layout` won't be called. This way,
incorrect measurements won't be applied and next native layout will
remeasure & apply correct layout. Ultimately, we want the screen to be
laid out fully by native layout, not by Yoga, so this is the desired
behavior. If anything changes, `StackHost` receives new measurement from
Yoga and will trigger native layout pass for `StackScreen`.

## Changes

- set `needsCustomLayoutForChildren=true` in `StackHostViewManager`

## Before & after - visual documentation

| Before | After |
| --- | --- |
| <video
src="https://github.com/user-attachments/assets/737b9b88-4749-439a-a970-271edeff051a"
/> | <video
src="https://github.com/user-attachments/assets/7c14be70-4a00-42c0-aba2-aea8d793cb2f"
/> |

## Test plan

Run `test-stack-toolbar-menu-show-as-action`. Change orientation
portrait -> landscape -> portrait multiple times. The content of the
screen should be laid out correctly.

## Checklist

- [x] Included code example that can be used to test this change.
- [x] For visual changes, included screenshots / GIFs / recordings
documenting the change.
- [x] Ensured that CI passes
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.

4 participants