Do not register OnBackInvokedCallback for SoftInputWindow.

By default Dialog#onStart will register onBackInvokedCallback, which
should be necessary since InputMethodService will register
CompatOnBackInvokedCallback for input method window.

Bug: 294162707
Test: atest BackGestureInvokedTest
Test: manual, launch Calendar then trigger back several times, verify
the back gesture can close app normally.

Change-Id: Icacfef60666fba1522f9d0f4ddc47e74a4560a32
This commit is contained in:
wilsonshih
2023-08-14 19:44:12 +08:00
parent 9815c0eb7b
commit c385181208
2 changed files with 19 additions and 3 deletions

View File

@@ -454,12 +454,11 @@ public class Dialog implements DialogInterface, Window.Callback,
*/
protected void onStart() {
if (mActionBar != null) mActionBar.setShowHideAnimationEnabled(true);
if (mContext != null
if (allowsRegisterDefaultOnBackInvokedCallback() && mContext != null
&& WindowOnBackInvokedDispatcher.isOnBackInvokedCallbackEnabled(mContext)) {
// Add onBackPressed as default back behavior.
mDefaultBackCallback = this::onBackPressed;
getOnBackInvokedDispatcher().registerSystemOnBackInvokedCallback(mDefaultBackCallback);
mDefaultBackCallback = null;
}
}
@@ -470,9 +469,18 @@ public class Dialog implements DialogInterface, Window.Callback,
if (mActionBar != null) mActionBar.setShowHideAnimationEnabled(false);
if (mDefaultBackCallback != null) {
getOnBackInvokedDispatcher().unregisterOnBackInvokedCallback(mDefaultBackCallback);
mDefaultBackCallback = null;
}
}
/**
* Whether this dialog allows to register the default onBackInvokedCallback.
* @hide
*/
protected boolean allowsRegisterDefaultOnBackInvokedCallback() {
return true;
}
private static final String DIALOG_SHOWING_TAG = "android:dialogShowing";
private static final String DIALOG_HIERARCHY_TAG = "android:dialogHierarchy";
@@ -697,7 +705,8 @@ public class Dialog implements DialogInterface, Window.Callback,
if (event.isTracking() && !event.isCanceled()) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (!WindowOnBackInvokedDispatcher.isOnBackInvokedCallbackEnabled(mContext)) {
if (!WindowOnBackInvokedDispatcher.isOnBackInvokedCallbackEnabled(mContext)
|| !allowsRegisterDefaultOnBackInvokedCallback()) {
onBackPressed();
return true;
}

View File

@@ -79,6 +79,13 @@ final class SoftInputWindow extends Dialog {
@WindowState
private int mWindowState = WindowState.TOKEN_PENDING;
@Override
protected boolean allowsRegisterDefaultOnBackInvokedCallback() {
// Do not register OnBackInvokedCallback from Dialog#onStart, InputMethodService will
// register CompatOnBackInvokedCallback for input method window.
return false;
}
/**
* Set {@link IBinder} window token to the window.
*