Skip to content

fix(reader): let the newest reader widget own the visual navigator - #332

Merged
ddfreiling merged 6 commits into
Notalib:mainfrom
ddfreiling:fix/android-fast-reopen-crash
Nov 18, 2026
Merged

ddfreiling merged 6 commits into
Notalib:mainfrom
ddfreiling:fix/android-fast-reopen-crash

Conversation

@ddfreiling

Copy link
Copy Markdown
Member

Summary

Reopening a book quickly on Android crashed with IllegalStateException: Can't change container ID of fragment EpubReaderFragment. While the outgoing player page animates out, a new ReadiumReaderWidget mounts for the next book before the old one is disposed. Both call epubEnable, and the singleton navigator's fragment was re-added to the second widget's container.

The same overlap broke the reader on both platforms without crashing: the old widget's Dart _cleanup nulled currentReaderWidget after the new one registered, so goForward, preferences and decorations went nowhere.

Change

  • Android core: the newest reader widget owns the visual navigator, matching iOS (clearCurrentReaderView(ifIs:)). claimVisualNavigator runs at the start of the main-thread part of epubEnable/pdfEnable/comicEnable. A superseded widget bails out, and a navigator owned by an older widget is disposed first. visualClose(widget) only tears down the navigator if that widget owns it. Navigator dispose() skips a fragment that is no longer added.
  • Dart core: ReadiumReaderWidget._cleanup only clears currentReaderWidget if it is still this widget.
  • Example app: ClosePublication carries the publication it closes and is ignored once another one is opening or open, so the delayed close after pop no longer closes the next book. ReaderWidget stops rebuilding once its route is no longer active, so an outgoing page does not mount a second reader.
  • Removed the unused epubClose/pdfClose/comicClose.

Review notes

  • The claim stays in each enable, after the per-type positions preload suspension. Hoisting it before the suspension would let a newer widget claim and then be overtaken by the older one finishing its preload.
  • A superseded widget whose enable bails out stays in its loading state. In practice it is the outgoing page and gets disposed right after. If an app keeps two readers alive and the older one becomes the newest again, it will not re-enable by itself.
  • The older widget's dispose still emits ReaderStatus.Closed while the new reader is open, as on iOS. Left for a follow-up so both platforms change together.
  • Example: pressing back while the next book is still loading leaves that book open in the bloc. Not addressed here.

Verification

  • Reproduced the crash on main on a Galaxy S23 by going back and opening another book within ~100 ms (adb input).
  • With the fix: repeated fast reopens without a crash, one reader each, the reader shows the new book, and goForward moves it. A slow reopen (3 s) also works.
  • ktlint --format, ./gradlew :flutter_readium:compileDebugKotlin, bin/format, bin/analyze, bin/changelog check.

Not covered

  • iOS was not run. The Dart _cleanup change applies there too.
  • PDF and comic fast reopen were not run on a device; they share the claim path with EPUB.

🤖 Generated with Claude Code

ddfreiling and others added 2 commits November 14, 2026 04:32
Reopening a book within ~400 ms of going back mounted a second
ReadiumReaderWidget before the outgoing one was disposed: the example's
outgoing player page rebuilt for the new publication from the shared
PublicationBloc. On Android the second widget re-added the singleton
navigator's fragment to its own container and crashed with "Can't
change container ID of fragment". The outgoing widget's dispose then
cleared the Dart current reader and disposed the native navigator that
the new widget relied on.

- Android: track which widget owns the visual navigator. A superseded
  widget no longer enables it, a newer widget replaces an older
  widget's navigator instead of re-attaching its fragment, and
  visualClose only disposes the caller's own navigator. Applies to
  EPUB, PDF and comic.
- Dart: an older reader's dispose only unregisters itself, matching
  iOS clearCurrentReaderView(ifIs:).
- Example: the outgoing page no longer rebuilds its reader, and the
  delayed ClosePublication is ignored once another book is opening,
  instead of closing the newly opened one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A superseded reader widget no longer resets narrationSyncEnabled for
the newer reader, since the seed now runs after claimVisualNavigator.
The example's ReaderWidget gates rebuilds on ModalRoute.isActive, which
is false while the route animates out but true under sheets and pushed
routes. Remove the unused epubClose/pdfClose/comicClose.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ddfreiling
ddfreiling requested review from m-abs and a balanced review from Copilot November 17, 2026 08:02

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.

🟡 Changes recommended

The inactive-route guard misses the initial build, allowing a second native reader to mount during audiobook navigation and compete for singleton ownership.

1 open issue
What changed in this PR

This PR addresses fast book-reopening failures by making the newest reader widget own the shared visual navigator and preventing older widgets from clearing its registration.

Changes:

  • Add Android navigator ownership checks and safer fragment disposal.
  • Preserve the newer Dart reader registration when an older widget is disposed.
  • Scope delayed example-app close events to their publication and limit outgoing-route rebuilds.
File Description
flutter_readium/​lib/​reader_widget.dart Only clears registration when the disposed widget still owns it.
flutter_readium/​example/​lib/​widgets/​reader.widget.dart Filters publication updates on inactive routes.
flutter_readium/​example/​lib/​state/​publication_bloc.dart Ignores stale close events and centralizes subscription cleanup.
flutter_readium/​example/​lib/​pages/​player.page.dart Captures the publication targeted by a delayed close.
flutter_readium/​changelog.d/​+reader-overlap-reopen.fixed.md Documents the overlapping-reader fix.
flutter_readium/​android/​src/​main/​kotlin/​dk/​nota/​flutterreadium/​ReadiumReaderWidget.kt Passes widget identity when closing the navigator.
flutter_readium/​android/​src/​main/​kotlin/​dk/​nota/​flutterreadium/​ReadiumReader.kt Adds ownership-aware navigator claiming and teardown; removes unused close methods.
flutter_readium/​android/​src/​main/​kotlin/​dk/​nota/​flutterreadium/​navigators/​PdfNavigator.kt Skips fragment removal when it is no longer added.
flutter_readium/​android/​src/​main/​kotlin/​dk/​nota/​flutterreadium/​navigators/​EpubNavigator.kt Skips fragment removal when it is no longer added.
flutter_readium/​android/​src/​main/​kotlin/​dk/​nota/​flutterreadium/​navigators/​ComicNavigator.kt Skips fragment removal when it is no longer added.

Review effort: Balanced

ddfreiling and others added 2 commits November 17, 2026 10:47
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
ddfreiling and others added 2 commits November 17, 2026 11:50
The outer PublicationBloc builder could swap an outgoing audiobook page
to a new ReaderWidget, whose first build ignores buildWhen and mounts a
second native reader.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ddfreiling
ddfreiling merged commit e15a167 into Notalib:main Nov 18, 2026
18 of 19 checks passed
@ddfreiling
ddfreiling deleted the fix/android-fast-reopen-crash branch November 18, 2026 09:50
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.

3 participants