Skip to content

Instantly share code, notes, and snippets.

@KaustubhPatange
Last active February 8, 2023 15:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KaustubhPatange/5614830f15faf1834cecbb5d57e137a3 to your computer and use it in GitHub Desktop.
Save KaustubhPatange/5614830f15faf1834cecbb5d57e137a3 to your computer and use it in GitHub Desktop.
// 1.
class NativePreImeInputStage ... {
...
@Override
protected int onProcess(QueuedInputEvent q) {
...
// If the new back dispatch is enabled, intercept KEYCODE_BACK before it reaches the
// view tree or IME, and invoke the appropriate {@link OnBackInvokedCallback}.
if (isBack(event) ... ) {
OnBackInvokedCallback topCallback = getOnBackInvokedDispatcher().getTopCallback(); // check below code
if (event.getAction() == KeyEvent.ACTION_UP) {
if (topCallback != null) {
topCallback.onBackInvoked(); // <-- Called here
return FINISH_HANDLED;
}
}
}
...
}
...
}
// 2.
class WindowOnBackInvokedDispatcher ... {
...
// This method is only available in WindowOnBackInvokedDispatcher & it returns the
// latest `onBackInvokedCallback` registered through `registerOnBackInvokedCallback`.
public OnBackInvokedCallback getTopCallback() {
if (mAllCallbacks.isEmpty()) {
return null;
}
for (Integer priority : mOnBackInvokedCallbacks.descendingKeySet()) {
ArrayList<OnBackInvokedCallback> callbacks = mOnBackInvokedCallbacks.get(priority);
if (!callbacks.isEmpty()) {
return callbacks.get(callbacks.size() - 1);
}
}
return null;
}
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment