Flutter Pro Design

Small details that build taste in Flutter.

curated by Kamran BekirovKamran Bekirov

When Android back button doesn't close your modals

A quick backstory from real life:

I opened my expen.app on a Xiaomi 12 (Android 15) and noticed the full-screen sheets don't close with the back button or edge swipe. On the Pixel 9 emulator (Android 16) though, it worked fine. What's going on?

After a bit of research, I came across OnBackInvokedCallback. Android 13 introduced it as a new way to dispatch back events, and it also powers "predictive back" (the edge-swipe gesture). For backward compatibility, Google made it opt-in. Adding this to AndroidManifest.xml fixed it for me:

<application
    ...
    android:enableOnBackInvokedCallback="true">

Sheets, dialogs, modals on Xiaomi now close with back press or edge swipe.

Watch out: this flag breaks WillPopScope. Migrate to PopScope first (Flutter migration guide).

The part I still don't fully get: I have another Flutter app of mine on the same Xiaomi. Without the flag, its back button closes modals just fine. The logcat for that app shows the exact same warning:

W/WindowOnBackDispatcher: OnBackInvokedCallback is not enabled for the application.
W/WindowOnBackDispatcher: Set 'android:enableOnBackInvokedCallback="true"' in the application manifest.

Same warning, same Android version, same Flutter version, MIUI delivering the back the same way. But one app's modals close, the other's don't. I tried bumping androidx.activity to match the working app's version, no change. Tried removing a custom Overlay wrapper, no change. Something in the expen.app's widget tree or plugins is consuming back differently and I couldn't track it down.

So if you hit this: add the flag, ship it. The fix works. The deeper cause goes beyond "Xiaomi gates the gesture", it apparently depends on your app's internals in ways I don't fully understand yet.