From 68a15a81423df20a66cd5cab3cf277d7a282bfba Mon Sep 17 00:00:00 2001 From: Thomas Taschauer <128734+TomTasche@users.noreply.github.com> Date: Sun, 20 Jul 2025 09:11:53 +0200 Subject: [PATCH] Fix back button navigation for internally opened documents MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When opening a document from within the app (via "Open File" or "Recent Documents"), pressing back now returns to the home screen instead of closing the app. External file opening behavior remains unchanged - back still returns to the calling app. Fixes #392 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- .../reader/ui/activity/MainActivity.java | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/app/src/main/java/at/tomtasche/reader/ui/activity/MainActivity.java b/app/src/main/java/at/tomtasche/reader/ui/activity/MainActivity.java index 81aa4f3248bf..cdcf1b9b5d70 100644 --- a/app/src/main/java/at/tomtasche/reader/ui/activity/MainActivity.java +++ b/app/src/main/java/at/tomtasche/reader/ui/activity/MainActivity.java @@ -557,6 +557,36 @@ public boolean onKeyDown(int keyCode, KeyEvent event) { return super.onKeyDown(keyCode, event); } + @Override + public void onBackPressed() { + // Check if document is displayed and was opened internally + if (documentFragment != null && documentContainer.getVisibility() == View.VISIBLE) { + // Check if the activity was launched without external data (internal file selection) + if (getIntent().getData() == null) { + // Return to landing screen instead of exiting + landingContainer.setVisibility(View.VISIBLE); + documentContainer.setVisibility(View.GONE); + + // Clear the document fragment + getSupportFragmentManager() + .beginTransaction() + .remove(documentFragment) + .commitNow(); + documentFragment = null; + + // Clear the lastUri to reset state + lastUri = null; + + analyticsManager.setCurrentScreen(this, "screen_main"); + + return; + } + } + + // Default behavior for external files or when no document is shown + super.onBackPressed(); + } + public void findDocument() { final Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); intent.addCategory(Intent.CATEGORY_OPENABLE);