Skip to content

Commit 55f47df

Browse files
CopilotTomTasche
andcommitted
Implement back button navigation fix for internally opened documents
Co-authored-by: TomTasche <128734+TomTasche@users.noreply.github.com>
1 parent e1d974e commit 55f47df

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import android.widget.CompoundButton;
2929
import android.widget.LinearLayout;
3030

31+
import androidx.activity.OnBackPressedCallback;
3132
import androidx.annotation.NonNull;
3233
import androidx.annotation.Nullable;
3334
import androidx.annotation.VisibleForTesting;
@@ -101,6 +102,7 @@ private static boolean isTesting() {
101102
private Uri lastUri;
102103
private Uri loadOnStart;
103104
private Uri lastSaveUri;
105+
private boolean documentOpenedInternally = false;
104106

105107
@Nullable
106108
private CountingIdlingResource openFileIdlingResource;
@@ -182,6 +184,7 @@ public void onClick(View view) {
182184
// (i.e. after bringing app back from background)
183185
if (getIntent().getData() != null) {
184186
loadOnStart = getIntent().getData();
187+
documentOpenedInternally = false; // External launch
185188

186189
analyticsManager.report(FirebaseAnalytics.Event.SELECT_CONTENT, FirebaseAnalytics.Param.CONTENT_TYPE, "other");
187190
} else {
@@ -194,6 +197,26 @@ public void onClick(View view) {
194197
}
195198

196199
addMenuProvider(this, this);
200+
201+
// Set up back button handling
202+
getOnBackPressedDispatcher().addCallback(this, new OnBackPressedCallback(true) {
203+
@Override
204+
public void handleOnBackPressed() {
205+
if (fullscreen) {
206+
leaveFullscreen();
207+
return;
208+
}
209+
210+
// If document is currently displayed and was opened internally, go back to landing
211+
if (documentContainer.getVisibility() == View.VISIBLE && documentOpenedInternally) {
212+
showLandingScreen();
213+
} else {
214+
// For external launches or when already on landing screen, use default behavior (close app)
215+
setEnabled(false);
216+
getOnBackPressedDispatcher().onBackPressed();
217+
}
218+
}
219+
});
197220
}
198221

199222
@Override
@@ -321,6 +344,7 @@ protected void onNewIntent(Intent intent) {
321344
if (intent.getData() != null) {
322345
crashManager.log("onNewIntent loadUri");
323346

347+
documentOpenedInternally = false; // External launch
324348
loadUri(intent.getData());
325349

326350
analyticsManager.report(FirebaseAnalytics.Event.SELECT_CONTENT, FirebaseAnalytics.Param.CONTENT_TYPE, "other");
@@ -606,6 +630,8 @@ public void onClick(DialogInterface dialog, int which) {
606630
openFileIdlingResource.increment();
607631
}
608632

633+
// Mark that the next document will be opened internally
634+
documentOpenedInternally = true;
609635
startActivityForResult(intent, 42);
610636
} catch (Exception e) {
611637
if (null != openFileIdlingResource) {
@@ -663,6 +689,21 @@ protected void onDestroy() {
663689
}
664690
}
665691

692+
private void showLandingScreen() {
693+
landingContainer.setVisibility(View.VISIBLE);
694+
documentContainer.setVisibility(View.GONE);
695+
696+
// Clear the document state
697+
lastUri = null;
698+
lastSaveUri = null;
699+
documentOpenedInternally = false;
700+
701+
// Clear the action bar title
702+
setTitle("");
703+
704+
analyticsManager.setCurrentScreen(this, "screen_main");
705+
}
706+
666707
public CrashManager getCrashManager() {
667708
return crashManager;
668709
}

0 commit comments

Comments
 (0)