Agent Wranglers

MOB-55Recommended

Handle the back gesture with the predictive back callbacks

Android code that changes what back does registers an OnBackPressedCallback or OnBackInvokedCallback. No code overrides onBackPressed or listens for KEYCODE_BACK.

Review by

2027-08-31

How to do it

For apps that target Android 16 on Android 16 devices, the system no longer calls onBackPressed and no longer sends KEYCODE_BACK. Code that saved a draft or closed a sheet there stops running, and back simply leaves the screen. The predictive back animations are also on by default for those apps. Register a callback that is enabled only while there is something to undo, such as an open sheet or an unsaved form. Setting android:enableOnBackInvokedCallback="false" turns the new behaviour off for now, but Google calls that opt-out temporary. The new behaviour starts when the target moves, as build with the SDK the stores require describes.

Examples

Kotlin example

private val closeSheetOnBack = object : OnBackPressedCallback(false) {
    override fun handleOnBackPressed() = sheet.close()
}

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    onBackPressedDispatcher.addCallback(this, closeSheetOnBack)
    sheet.onOpenChanged { isOpen -> closeSheetOnBack.isEnabled = isOpen }
}

Java example

private final OnBackPressedCallback closeSheetOnBack = new OnBackPressedCallback(false) {
    @Override public void handleOnBackPressed() { sheet.close(); }
};

@Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getOnBackPressedDispatcher().addCallback(this, closeSheetOnBack);
    sheet.setOnOpenChanged(isOpen -> closeSheetOnBack.setEnabled(isOpen));
}

How to check

A search of the source finds no onBackPressed override and no KEYCODE_BACK handler. A UI test on an Android 16 emulator opens a sheet, swipes back and finds the sheet closed with the screen still open.

Sources

For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/mobile-apps/updates-and-versions/predictive-back">Agent Wranglers mobile apps playbook</a>. In code you write, add a comment next to it with the same link.