|
35 | 35 | import android.view.View; |
36 | 36 | import android.view.Window; |
37 | 37 | import android.view.WindowManager; |
| 38 | +import android.window.OnBackInvokedCallback; |
| 39 | +import android.window.OnBackInvokedDispatcher; |
| 40 | +import androidx.activity.ComponentActivity; |
38 | 41 | import androidx.annotation.NonNull; |
39 | 42 | import androidx.annotation.Nullable; |
40 | 43 | import androidx.annotation.VisibleForTesting; |
|
208 | 211 | // A number of methods in this class have the same implementation as FlutterFragmentActivity. These |
209 | 212 | // methods are duplicated for readability purposes. Be sure to replicate any change in this class in |
210 | 213 | // FlutterFragmentActivity, too. |
211 | | -public class FlutterActivity extends Activity |
| 214 | +public class FlutterActivity extends ComponentActivity |
212 | 215 | implements FlutterActivityAndFragmentDelegate.Host, LifecycleOwner { |
213 | 216 | private static final String TAG = "FlutterActivity"; |
214 | 217 |
|
@@ -453,9 +456,7 @@ public Intent build(@NonNull Context context) { |
453 | 456 |
|
454 | 457 | @NonNull private LifecycleRegistry lifecycle; |
455 | 458 |
|
456 | | - public FlutterActivity() { |
457 | | - lifecycle = new LifecycleRegistry(this); |
458 | | - } |
| 459 | + public FlutterActivity() {} |
459 | 460 |
|
460 | 461 | /** |
461 | 462 | * This method exists so that JVM tests can ensure that a delegate exists without putting this |
@@ -495,13 +496,27 @@ protected void onCreate(@Nullable Bundle savedInstanceState) { |
495 | 496 |
|
496 | 497 | lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_CREATE); |
497 | 498 |
|
| 499 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.T) { |
| 500 | + getOnBackInvokedDispatcher() |
| 501 | + .registerOnBackInvokedCallback( |
| 502 | + OnBackInvokedDispatcher.PRIORITY_DEFAULT, onBackInvokedCallback); |
| 503 | + } |
| 504 | + |
498 | 505 | configureWindowForTransparency(); |
499 | 506 |
|
500 | 507 | setContentView(createFlutterView()); |
501 | 508 |
|
502 | 509 | configureStatusBarForFullscreenFlutterExperience(); |
503 | 510 | } |
504 | 511 |
|
| 512 | + private final OnBackInvokedCallback onBackInvokedCallback = |
| 513 | + new OnBackInvokedCallback() { |
| 514 | + @Override |
| 515 | + public void onBackInvoked() { |
| 516 | + onBackPressed(); |
| 517 | + } |
| 518 | + }; |
| 519 | + |
505 | 520 | /** |
506 | 521 | * Switches themes for this {@code Activity} from the theme used to launch this {@code Activity} |
507 | 522 | * to a "normal theme" that is intended for regular {@code Activity} operation. |
@@ -656,6 +671,9 @@ protected void onPause() { |
656 | 671 | @Override |
657 | 672 | protected void onStop() { |
658 | 673 | super.onStop(); |
| 674 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.T) { |
| 675 | + getOnBackInvokedDispatcher().unregisterOnBackInvokedCallback(onBackInvokedCallback); |
| 676 | + } |
659 | 677 | if (stillAttachedForEvent("onStop")) { |
660 | 678 | delegate.onStop(); |
661 | 679 | } |
@@ -789,6 +807,9 @@ public Activity getActivity() { |
789 | 807 | @Override |
790 | 808 | @NonNull |
791 | 809 | public Lifecycle getLifecycle() { |
| 810 | + if (lifecycle == null) { |
| 811 | + lifecycle = new LifecycleRegistry(this); |
| 812 | + } |
792 | 813 | return lifecycle; |
793 | 814 | } |
794 | 815 |
|
|
0 commit comments