From 5396352f039f14185d8aab236e7e6d91d18fd9a8 Mon Sep 17 00:00:00 2001 From: gumclaw Date: Wed, 1 Apr 2026 11:48:26 -0400 Subject: [PATCH 1/2] Fix IllegalStateException: Already closed in PDF viewer When switching view modes (single/continuous), the Pdf component was unmounted and remounted via key={viewMode}. This caused a race condition where the native PdfiumAndroid rendering thread was still rendering a page bitmap when the document was closed during unmount. Fix: explicitly unmount the Pdf component before changing the view mode, then remount on the next animation frame. This gives the native rendering thread time to complete before the document resources are released. --- app/pdf-viewer.tsx | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/app/pdf-viewer.tsx b/app/pdf-viewer.tsx index 378c719..09947aa 100644 --- a/app/pdf-viewer.tsx +++ b/app/pdf-viewer.tsx @@ -46,6 +46,15 @@ export default function PdfViewerScreen() { const [showTocModal, setShowTocModal] = useState(false); const [showViewModeModal, setShowViewModeModal] = useState(false); const [isSharing, setIsSharing] = useState(false); + const [pdfMounted, setPdfMounted] = useState(true); + + const switchViewMode = (mode: "single" | "continuous") => { + // Unmount the PDF component first to let the native rendering thread finish + // before the document is closed, avoiding IllegalStateException: Already closed + setPdfMounted(false); + setViewMode(mode); + requestAnimationFrame(() => setPdfMounted(true)); + }; useEffect(() => { if (Platform.OS === "android") { @@ -127,7 +136,7 @@ export default function PdfViewerScreen() { ), }} /> - + />} setShowTocModal(false)}> @@ -181,7 +190,7 @@ export default function PdfViewerScreen() { { - setViewMode("single"); + switchViewMode("single"); setShowViewModeModal(false); }} className="flex-row items-center justify-between border-b border-border py-4" @@ -194,7 +203,7 @@ export default function PdfViewerScreen() { { - setViewMode("continuous"); + switchViewMode("continuous"); setShowViewModeModal(false); }} className="flex-row items-center justify-between border-b border-border py-4" From dd3546e671365fa86c5629471323d36ad7305629 Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Wed, 1 Apr 2026 15:49:50 +0000 Subject: [PATCH 2/2] [autofix.ci] apply automated fixes --- app/pdf-viewer.tsx | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/app/pdf-viewer.tsx b/app/pdf-viewer.tsx index 09947aa..cf037c7 100644 --- a/app/pdf-viewer.tsx +++ b/app/pdf-viewer.tsx @@ -136,26 +136,28 @@ export default function PdfViewerScreen() { ), }} /> - {pdfMounted && { - setTotalPages(numberOfPages); - setTableOfContents(toc ?? []); - }} - onPageChanged={(page) => setCurrentPage(page)} - onError={(error) => { - Sentry.captureException(error); - console.error("PDF Error:", error); - }} - />} + {pdfMounted && ( + { + setTotalPages(numberOfPages); + setTableOfContents(toc ?? []); + }} + onPageChanged={(page) => setCurrentPage(page)} + onError={(error) => { + Sentry.captureException(error); + console.error("PDF Error:", error); + }} + /> + )} setShowTocModal(false)}>