Android back button in Flutter with go_router (and Android 16 predictive back)
flutterandroidback-buttonpopscopego_routerpredictive-backandroid-16
The Android back button is the most regression-prone part of the TLDR app: it broke several times, and each time the symptom was the worst one, the app closing on the first tap. The working setup is specific, and Android 16 made predictive back mandatory in a way Flutter's ShellRoute did not handle as of go_router 17.
Desired behavior
Back walks the app's own navigation history, then lands on the home tab (/posts), then shows
"press back again to exit", and only exits on a second press within 2 seconds.
The working configuration
PopScope(canPop: false, onPopInvokedWithResult: ...)wraps the shell Scaffold, and both it andWidgetsBindingObserver.didPopRoute()call one shared_handleBack(). PopScope is required because onceenableOnBackInvokedCallbackis in play (Android 14+),didPopRouteis bypassed.android:enableOnBackInvokedCallback="false"inAndroidManifest.xml.ZoomPageTransitionsBuilderin the theme's page transitions, notPredictiveBackPageTransitionsBuilder.- The shell keeps its own
_navHistorystack, socontext.go()tab switches can still be undone by back. - Screens with their own back semantics (post detail) wrap in
PopScope(canPop: false)and doif (context.canPop()) context.pop(); else context.go('/posts'). Make the fallback contextual: backing out of the AI composer (/ai/new) should land on/ai, not the main feed.
Android 16 finding (March 2026)
PredictiveBackPageTransitionsBuilder with enableOnBackInvokedCallback="true" exits the app
immediately on Android 16 (API 36), even with go_router 17 and PopScope(canPop: false) at the
root. PopScope on the root/ShellRoute does not propagate canPop to the system where predictive
back is mandatory. Upgrading go_router 14 to 17 did fix an older PopScope bug in branch routes
(no breaking changes, but run flutter clean after the upgrade or stale build caches throw
null-subtype errors). Do not re-enable predictive back until ShellRoute support is fixed upstream.
Process rule
Any change to the router, the app shell, the post detail screen, page transitions or the manifest can break back handling. Verify on two devices: an older Android (here a Pixel 2 XL, Android 11) and Android 16, before committing. Reduced-motion settings change transitions too (with the animator duration scale off, swipe navigation collapses to a plain slide; that is intended).
See also
- go_router navigation patterns and opening your own links in-app
- TLDR platform architecture (tldrnerd.com)