diff options
| -rw-r--r-- | core/java/android/widget/PopupWindow.java | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/core/java/android/widget/PopupWindow.java b/core/java/android/widget/PopupWindow.java index decd9a547edb..c1ec168af145 100644 --- a/core/java/android/widget/PopupWindow.java +++ b/core/java/android/widget/PopupWindow.java @@ -56,6 +56,9 @@ import android.view.WindowManager; import android.view.WindowManager.LayoutParams; import android.view.WindowManager.LayoutParams.SoftInputModeFlags; import android.view.WindowManagerGlobal; +import android.window.OnBackInvokedCallback; +import android.window.OnBackInvokedDispatcher; +import android.window.WindowOnBackInvokedDispatcher; import com.android.internal.R; @@ -277,6 +280,8 @@ public class PopupWindow { private boolean mPopupViewInitialLayoutDirectionInherited; + private OnBackInvokedCallback mBackCallback; + /** * <p>Create a new empty, non focusable popup window of dimension (0,0).</p> * @@ -2028,6 +2033,8 @@ public class PopupWindow { final PopupDecorView decorView = mDecorView; final View contentView = mContentView; + unregisterBackCallback(decorView.findOnBackInvokedDispatcher()); + final ViewGroup contentHolder; final ViewParent contentParent = contentView.getParent(); if (contentParent instanceof ViewGroup) { @@ -2082,6 +2089,15 @@ public class PopupWindow { } } + private void unregisterBackCallback(@Nullable OnBackInvokedDispatcher onBackInvokedDispatcher) { + OnBackInvokedCallback backCallback = mBackCallback; + mBackCallback = null; + if (onBackInvokedDispatcher != null && backCallback != null) { + onBackInvokedDispatcher.unregisterOnBackInvokedCallback( + backCallback); + } + } + /** * Returns the window-relative epicenter bounds to be used by enter and * exit transitions. @@ -2725,6 +2741,30 @@ public class PopupWindow { } } } + + @Override + protected void onAttachedToWindow() { + super.onAttachedToWindow(); + if (!WindowOnBackInvokedDispatcher.isOnBackInvokedCallbackEnabled(mContext)) { + return; + } + + OnBackInvokedDispatcher dispatcher = findOnBackInvokedDispatcher(); + if (dispatcher == null) { + return; + } + + mBackCallback = PopupWindow.this::dismiss; + + dispatcher.registerOnBackInvokedCallback(OnBackInvokedDispatcher.PRIORITY_DEFAULT, + mBackCallback); + } + + @Override + protected void onDetachedFromWindow() { + super.onDetachedFromWindow(); + unregisterBackCallback(findOnBackInvokedDispatcher()); + } } private class PopupBackgroundView extends FrameLayout { |