Skip to content

Commit 3df6fb3

Browse files
CopilotTomTasche
andcommitted
Use modern OnBackPressedCallback instead of deprecated onBackPressed
Co-authored-by: TomTasche <128734+TomTasche@users.noreply.github.com>
1 parent 2a7c5a8 commit 3df6fb3

1 file changed

Lines changed: 21 additions & 16 deletions

File tree

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

Lines changed: 21 additions & 16 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;
@@ -196,6 +197,26 @@ public void onClick(View view) {
196197
}
197198

198199
addMenuProvider(this, this);
200+
201+
// Set up modern 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+
});
199220
}
200221

201222
@Override
@@ -572,22 +593,6 @@ public boolean onKeyDown(int keyCode, KeyEvent event) {
572593
return super.onKeyDown(keyCode, event);
573594
}
574595

575-
@Override
576-
public void onBackPressed() {
577-
if (fullscreen) {
578-
leaveFullscreen();
579-
return;
580-
}
581-
582-
// If document is currently displayed and was opened internally, go back to landing
583-
if (documentContainer.getVisibility() == View.VISIBLE && documentOpenedInternally) {
584-
showLandingScreen();
585-
} else {
586-
// For external launches or when already on landing screen, use default behavior (close app)
587-
super.onBackPressed();
588-
}
589-
}
590-
591596
public void findDocument() {
592597
final Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
593598
intent.addCategory(Intent.CATEGORY_OPENABLE);

0 commit comments

Comments
 (0)