|
28 | 28 | import android.widget.CompoundButton; |
29 | 29 | import android.widget.LinearLayout; |
30 | 30 |
|
| 31 | +import androidx.activity.OnBackPressedCallback; |
31 | 32 | import androidx.annotation.NonNull; |
32 | 33 | import androidx.annotation.Nullable; |
33 | 34 | import androidx.annotation.VisibleForTesting; |
@@ -196,6 +197,26 @@ public void onClick(View view) { |
196 | 197 | } |
197 | 198 |
|
198 | 199 | 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 | + }); |
199 | 220 | } |
200 | 221 |
|
201 | 222 | @Override |
@@ -572,22 +593,6 @@ public boolean onKeyDown(int keyCode, KeyEvent event) { |
572 | 593 | return super.onKeyDown(keyCode, event); |
573 | 594 | } |
574 | 595 |
|
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 | | - |
591 | 596 | public void findDocument() { |
592 | 597 | final Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); |
593 | 598 | intent.addCategory(Intent.CATEGORY_OPENABLE); |
|
0 commit comments