Skip to content

fix(expo): recreate Android recomposer after reattach#9143

Open
D3OXY wants to merge 2 commits into
clerk:mainfrom
D3OXY:d3oxy/fix-expo-android-compose-reattach
Open

fix(expo): recreate Android recomposer after reattach#9143
D3OXY wants to merge 2 commits into
clerk:mainfrom
D3OXY:d3oxy/fix-expo-android-compose-reattach

Conversation

@D3OXY

@D3OXY D3OXY commented Jul 13, 2026

Copy link
Copy Markdown

Description

Fixes #9142

Android native Clerk views cancel their parent Recomposer whenever the host temporarily detaches from its window. React Navigation can later reattach the same native view, leaving Compose content bound to a canceled parent composition context.

This change makes the shared Android host own the complete composition lifecycle:

  • setupView() ensures a live recomposer exists before setting Compose content, including when React Native Fabric updates props or measures content before window attachment;
  • window attachment recreates content for every Compose-backed Clerk view, including AuthView, UserButton, and UserProfileView;
  • window detachment disposes the composition and cancels and clears recomposer state;
  • recovery checks the launched recomposition job rather than its parent supervisor job.

iOS is unchanged.

Validation

  • Patched consumer app on Android API 36:
    • cold start → profile image and Account UI work;
    • details route → back → profile image and Account UI still work;
    • AI Chat tab → Home tab → profile image and Account UI still work;
    • repeated detach/reattach cycles remain interactive.
  • Verified the Fabric pre-attachment failure path no longer sets Compose content without first assigning a live parent recomposer.
  • pnpm --filter @clerk/expo... build
  • pnpm --filter @clerk/expo test — 92 tests passed
  • pnpm --filter @clerk/expo lint — no errors
  • pnpm --filter @clerk/expo format:check
  • Expo SDK 57 Android release fixture — BUILD SUCCESSFUL

Checklist

  • Full repository pnpm test runs as expected; targeted Expo tests pass.
  • Full repository pnpm build runs as expected; filtered Expo dependency build passes.
  • JSDoc is not applicable; no public API changed.
  • Documentation is not applicable; this restores intended lifecycle behavior.

Type of change

  • 🐛 Bug fix
  • 🌟 New feature
  • 🔨 Breaking change
  • 📖 Refactoring / dependency upgrade / documentation
  • other:

Summary by CodeRabbit

  • Bug Fixes
    • Fixed an Android issue where native UI components could crash before window attachment or become unresponsive after navigation and view reattach.
    • Improved Compose native view lifecycle handling by starting rendering only when attached to a window and properly disposing and canceling rendering on detachment to prevent stale state.

@vercel

vercel Bot commented Jul 13, 2026

Copy link
Copy Markdown

@D3OXY is attempting to deploy a commit to the Clerk Production Team on Vercel.

A member of the Team first needs to authorize it.

@changeset-bot

changeset-bot Bot commented Jul 13, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 4149758

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@clerk/expo Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@coderabbitai

coderabbitai Bot commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The Android Compose native view host now defers recomposer creation until window attachment, recreates recomposition after reattachment, disposes composition state during detachment, and cancels stale resources. A Changesets entry records a patch release for @clerk/expo.

Changes

Expo Compose lifecycle

Layer / File(s) Summary
Lifecycle cleanup and restart entry points
packages/expo/android/src/main/java/expo/modules/clerk/ClerkComposeNativeViewHost.kt, packages/expo/android/src/main/java/expo/modules/clerk/ClerkUserButtonViewModule.kt
Compose setup no longer initializes the recomposer during construction. Recomposition starts from the host’s onAttachedToWindow(), while the UserButton-specific attachment override is removed. Detachment disposes the composition, cancels and clears recomposer resources, then invokes host detachment handling.
Recomposer recreation and release note
packages/expo/android/src/main/java/expo/modules/clerk/ClerkComposeNativeViewHost.kt, .changeset/fuzzy-lions-recompose.md
startRecomposer() guards lifecycle state, creates a main-dispatcher recomposer, assigns it to composeView, launches recomposition, and records a patch release for @clerk/expo.

Estimated code review effort: 2 (Simple) | ~10 minutes

Suggested labels: expo

Suggested reviewers: wobsoriano

Poem

A bunny saw views detach and fly,
Then made fresh Compose bloom nearby.
Old jobs hopped out, new jobs began,
The button danced across the span.
“Reattach!” cried Bun, “and render bright!”

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Linked Issues check ✅ Passed The patch matches #9142 by disposing the old composition, clearing the canceled recomposer, and creating a fresh one on attach.
Out of Scope Changes check ✅ Passed The only added non-code change is a changeset entry, and the code edits stay focused on the Android recomposer lifecycle fix.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: recreating the Android recomposer after reattachment.

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 2

🧹 Nitpick comments (1)
packages/expo/android/src/main/java/expo/modules/clerk/ClerkComposeNativeViewHost.kt (1)

27-27: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Prefer importing kotlinx.coroutines.Job/SupervisorJob instead of fully-qualifying inline.

Fully-qualified references (kotlinx.coroutines.Job, kotlinx.coroutines.SupervisorJob) are repeated across the field declaration and startRecomposer(); importing them would improve readability.

Also applies to: 46-46, 54-54, 62-63, 65-65

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@packages/expo/android/src/main/java/expo/modules/clerk/ClerkComposeNativeViewHost.kt`
at line 27, Replace the fully qualified kotlinx.coroutines.Job and
kotlinx.coroutines.SupervisorJob references in ClerkComposeNativeViewHost,
including recomposerJob and startRecomposer(), with imports at the top of the
file. Preserve the existing coroutine behavior and declarations.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In
`@packages/expo/android/src/main/java/expo/modules/clerk/ClerkComposeNativeViewHost.kt`:
- Around line 53-67: Update startRecomposer so recomposerJob references the Job
returned by the coroutine launched to run
newRecomposer.runRecomposeAndApplyChanges(), rather than the standalone
SupervisorJob in the scope context. Preserve the existing scope and recomposer
setup, while ensuring the isActive guard permits recovery after the
recomposition coroutine completes or fails.
- Around line 53-67: Update ClerkAuthNativeView and ClerkUserProfileNativeView
so onAttachedToWindow() invokes setupView(), matching ClerkUserButtonNativeView.
Preserve the existing OnViewDidUpdateProps behavior and ensure reattaching
without prop changes recreates the Compose content after the recomposer
restarts.

---

Nitpick comments:
In
`@packages/expo/android/src/main/java/expo/modules/clerk/ClerkComposeNativeViewHost.kt`:
- Line 27: Replace the fully qualified kotlinx.coroutines.Job and
kotlinx.coroutines.SupervisorJob references in ClerkComposeNativeViewHost,
including recomposerJob and startRecomposer(), with imports at the top of the
file. Preserve the existing coroutine behavior and declarations.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository YAML (base), Repository UI (inherited)

Review profile: CHILL

Plan: Pro Plus

Run ID: c8505747-f658-4f76-b111-5055c5f794dd

📥 Commits

Reviewing files that changed from the base of the PR and between 1d0e78c and fbfa349.

📒 Files selected for processing (2)
  • .changeset/fuzzy-lions-recompose.md
  • packages/expo/android/src/main/java/expo/modules/clerk/ClerkComposeNativeViewHost.kt

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.

[Expo] Android native UserButton becomes inert after navigation detach

1 participant