Skip to content

Commit 68a15a8

Browse files
TomTascheclaude
andcommitted
Fix back button navigation for internally opened documents
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 <noreply@anthropic.com>
1 parent 60eb5d4 commit 68a15a8

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

app/src/main/java/at/tomtasche/reader/ui/activity/MainActivity.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,36 @@ public boolean onKeyDown(int keyCode, KeyEvent event) {
557557
return super.onKeyDown(keyCode, event);
558558
}
559559

560+
@Override
561+
public void onBackPressed() {
562+
// Check if document is displayed and was opened internally
563+
if (documentFragment != null && documentContainer.getVisibility() == View.VISIBLE) {
564+
// Check if the activity was launched without external data (internal file selection)
565+
if (getIntent().getData() == null) {
566+
// Return to landing screen instead of exiting
567+
landingContainer.setVisibility(View.VISIBLE);
568+
documentContainer.setVisibility(View.GONE);
569+
570+
// Clear the document fragment
571+
getSupportFragmentManager()
572+
.beginTransaction()
573+
.remove(documentFragment)
574+
.commitNow();
575+
documentFragment = null;
576+
577+
// Clear the lastUri to reset state
578+
lastUri = null;
579+
580+
analyticsManager.setCurrentScreen(this, "screen_main");
581+
582+
return;
583+
}
584+
}
585+
586+
// Default behavior for external files or when no document is shown
587+
super.onBackPressed();
588+
}
589+
560590
public void findDocument() {
561591
final Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
562592
intent.addCategory(Intent.CATEGORY_OPENABLE);

0 commit comments

Comments
 (0)