Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 13d02f1

Browse files
committed
Implement OnBackInvoked___
1 parent ebf6503 commit 13d02f1

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

shell/platform/android/io/flutter/embedding/android/FlutterActivity.java

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
import android.view.View;
3636
import android.view.Window;
3737
import android.view.WindowManager;
38+
import android.window.OnBackInvokedCallback;
39+
import android.window.OnBackInvokedDispatcher;
40+
import androidx.activity.ComponentActivity;
3841
import androidx.annotation.NonNull;
3942
import androidx.annotation.Nullable;
4043
import androidx.annotation.VisibleForTesting;
@@ -208,7 +211,7 @@
208211
// A number of methods in this class have the same implementation as FlutterFragmentActivity. These
209212
// methods are duplicated for readability purposes. Be sure to replicate any change in this class in
210213
// FlutterFragmentActivity, too.
211-
public class FlutterActivity extends Activity
214+
public class FlutterActivity extends ComponentActivity
212215
implements FlutterActivityAndFragmentDelegate.Host, LifecycleOwner {
213216
private static final String TAG = "FlutterActivity";
214217

@@ -453,9 +456,7 @@ public Intent build(@NonNull Context context) {
453456

454457
@NonNull private LifecycleRegistry lifecycle;
455458

456-
public FlutterActivity() {
457-
lifecycle = new LifecycleRegistry(this);
458-
}
459+
public FlutterActivity() {}
459460

460461
/**
461462
* 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) {
495496

496497
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_CREATE);
497498

499+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.T) {
500+
getOnBackInvokedDispatcher()
501+
.registerOnBackInvokedCallback(
502+
OnBackInvokedDispatcher.PRIORITY_DEFAULT, onBackInvokedCallback);
503+
}
504+
498505
configureWindowForTransparency();
499506

500507
setContentView(createFlutterView());
501508

502509
configureStatusBarForFullscreenFlutterExperience();
503510
}
504511

512+
private final OnBackInvokedCallback onBackInvokedCallback =
513+
new OnBackInvokedCallback() {
514+
@Override
515+
public void onBackInvoked() {
516+
onBackPressed();
517+
}
518+
};
519+
505520
/**
506521
* Switches themes for this {@code Activity} from the theme used to launch this {@code Activity}
507522
* to a "normal theme" that is intended for regular {@code Activity} operation.
@@ -656,6 +671,9 @@ protected void onPause() {
656671
@Override
657672
protected void onStop() {
658673
super.onStop();
674+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.T) {
675+
getOnBackInvokedDispatcher().unregisterOnBackInvokedCallback(onBackInvokedCallback);
676+
}
659677
if (stillAttachedForEvent("onStop")) {
660678
delegate.onStop();
661679
}
@@ -789,6 +807,9 @@ public Activity getActivity() {
789807
@Override
790808
@NonNull
791809
public Lifecycle getLifecycle() {
810+
if (lifecycle == null) {
811+
lifecycle = new LifecycleRegistry(this);
812+
}
792813
return lifecycle;
793814
}
794815

0 commit comments

Comments
 (0)