Repository navigation
Conversation
…state The pager's internal ComposeView has no view id, so Compose saved its rememberSaveable state (the pager's current page, the page hosts' view state) in the host Fragment's SavedStateRegistry under the shared key "SaveableStateRegistry:-1". When react-native-screens destroys the Fragment view of a covered screen, that state is saved, and a pager mounted afterwards restores it: it starts on the old page and ignores initialPage. Give the ComposeView a SavedStateRegistryOwner from the same private owner that already provides its Lifecycle. Its registry starts empty and is never saved, so nothing is persisted or restored. React owns the pager state, so nothing is lost. A unique view id would also avoid the collision, but every remount would leave an orphaned entry in the Fragment's saved state. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
The focused change isolates saved state with correct lifecycle initialization and no identified blocking issues.
No open issues 🎉
What changed in this PR
Isolates Android pager Compose saved state from the host Fragment, preventing remounted pagers from restoring another pager’s page instead of using initialPage.
Changes:
- Gives each pager’s existing lifecycle owner a private saved-state registry.
- Initializes the registry without restored state and assigns it to the Compose view.
| File | Description |
|---|---|
| android/src/main/java/com/reactnativepagerview/ComposePagerView.kt | Adds and connects a private saved-state registry while preserving lifecycle behavior. |
Review effort: Balanced
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
Problem. Since 9.0, each
PagerViewon Android draws through a Jetpack Compose view inside the native view. Compose stores some state withrememberSaveable, including the pager's current page. That state goes into the host Fragment's saved-state store, keyed by the view's id. The pager's internal view has no id, so every pager in the app uses the same key (SaveableStateRegistry:-1).When react-native-screens destroys the view of a screen that's covered by another, it saves that state. The next pager to mount under that key restores the old page and ignores
initialPage. In our app, Home remounts its pager after you reorder feeds, so it opened on the old index and the tab bar didn't match the feed shown.Solution. The library already gives the Compose view its own private Lifecycle owner so that react-native-screens can't kill it. The fix makes that same object provide the saved-state store too. The store starts empty and is never written out, so nothing is saved or restored. That's fine because React already owns the pager's state (
initialPageand the page events).Test Plan
What's required for testing (prerequisites)?
What are the steps to reproduce (after prerequisites)?
Compatibility
Checklist
README.md