diff --git a/core/java/android/window/ImeOnBackInvokedDispatcher.java b/core/java/android/window/ImeOnBackInvokedDispatcher.java index 2a12507679f550107f839a704e6a1e04836380f7..ce1f9869b690b526782c9ff767784af6e752f16b 100644 --- a/core/java/android/window/ImeOnBackInvokedDispatcher.java +++ b/core/java/android/window/ImeOnBackInvokedDispatcher.java @@ -55,6 +55,9 @@ public class ImeOnBackInvokedDispatcher implements OnBackInvokedDispatcher, Parc static final int RESULT_CODE_UNREGISTER = 1; @NonNull private final ResultReceiver mResultReceiver; + // The handler to run callbacks on. This should be on the same thread + // the ViewRootImpl holding IME's WindowOnBackInvokedDispatcher is created on. + private Handler mHandler; public ImeOnBackInvokedDispatcher(Handler handler) { mResultReceiver = new ResultReceiver(handler) { @@ -68,6 +71,10 @@ public class ImeOnBackInvokedDispatcher implements OnBackInvokedDispatcher, Parc }; } + void setHandler(@NonNull Handler handler) { + mHandler = handler; + } + /** * Override this method to return the {@link WindowOnBackInvokedDispatcher} of the window * that should receive the forwarded callback. @@ -326,7 +333,7 @@ public class ImeOnBackInvokedDispatcher implements OnBackInvokedDispatcher, Parc @Override public void onBackInvoked() { - mCallback.onBackInvoked(); + mHandler.post(mCallback::onBackInvoked); } @Override @@ -336,7 +343,7 @@ public class ImeOnBackInvokedDispatcher implements OnBackInvokedDispatcher, Parc private void maybeRunOnAnimationCallback(Consumer<OnBackAnimationCallback> block) { if (mCallback instanceof OnBackAnimationCallback) { - block.accept((OnBackAnimationCallback) mCallback); + mHandler.post(() -> block.accept((OnBackAnimationCallback) mCallback)); } } } diff --git a/core/java/android/window/WindowOnBackInvokedDispatcher.java b/core/java/android/window/WindowOnBackInvokedDispatcher.java index 9f6933c5429de7227b833f2d61479a4b67a80377..4c993c2544cef88b7a0a3af7959e902e9e0c4ed7 100644 --- a/core/java/android/window/WindowOnBackInvokedDispatcher.java +++ b/core/java/android/window/WindowOnBackInvokedDispatcher.java @@ -550,6 +550,7 @@ public class WindowOnBackInvokedDispatcher implements OnBackInvokedDispatcher { public void setImeOnBackInvokedDispatcher( @NonNull ImeOnBackInvokedDispatcher imeDispatcher) { mImeDispatcher = imeDispatcher; + mImeDispatcher.setHandler(mHandler); } /** Returns true if a non-null {@link ImeOnBackInvokedDispatcher} has been set. **/