Flutter: How to Disable or Override the Android Back Button
In this article
The Android system back button can be intercepted in Flutter using WillPopScope (or its modern replacement PopScope). Wrap the screen you want to protect in the widget, return false from its callback to block the default pop, and run your own logic instead. This guide shows the exact steps for both widgets, plus how to handle gestures, nested navigators, and the mistakes that make the block silently fail.
What You'll Need
- A Flutter project with at least one screen pushed onto the navigator stack (a second page is required to test back behaviour).
- Flutter SDK installed and a working emulator or physical Android device. The back button is an Android concept; iOS has no hardware back button, so testing on Android matters.
- Basic familiarity with
Navigatorand widget build methods. - If you are on a recent Flutter version, use
PopScope.WillPopScopeis deprecated in newer releases but still works in older projects.
Step-by-Step
- Identify the screen that should block back. This is usually a form, a checkout page, a video player, or any screen where leaving accidentally would lose data. Only wrap that screen, not the whole app, unless you truly want global behaviour.
- Choose your widget. For Flutter versions that support it, use
PopScope. If your project is pinned to an older Flutter release, useWillPopScope. Do not mix them on the same route. - Wrap the screen's top-level widget. In the
buildmethod, returnPopScope(canPop: false, onPopInvokedWithResult: (didPop, result) { ... }, child: Scaffold(...)). Thechildis your existing widget tree. WithWillPopScope, the equivalent isWillPopScope(onWillPop: () async { return false; }, child: Scaffold(...)). - Decide what happens on back. Returning
false(or settingcanPop: false) blocks the pop. Inside the callback, run your own logic: show a confirmation dialog, save a draft, pause a video, or navigate to a different route withNavigator.pushReplacement. If you want to allow the pop conditionally, returntrueor setcanPopbased on a state variable. - Handle the confirmation dialog case. A common pattern is: user presses back, you show an
AlertDialogasking "Discard changes?", and if they confirm you callNavigator.of(context).pop()yourself. WithPopScope, note that callingpopinside the callback can re-trigger the callback; guard it with a boolean flag or useNavigator.popafter settingcanPopto true viasetState. - Test with the system back button and the back gesture. On Android 10 and later, the predictive back gesture (swipe from either edge) also triggers the same callback. Press the on-screen or hardware back button, then try the edge swipe. Both should hit your handler.
- Check nested navigators. If your app uses a nested
Navigator(for example inside a tab), thePopScopemust be placed inside the route that the nested navigator manages. APopScopeon the outer route will not intercept pops handled by the inner navigator. - Remove or relax the block when appropriate. If the block is only needed while a form is dirty, store a
bool _hasUnsavedChangesand setcanPop: !_hasUnsavedChanges. When the user saves, callsetStateso the next back press passes through.
Common Mistakes
- Wrapping the wrong widget.
PopScopeandWillPopScopeonly affect routes they are part of. Putting it aroundMaterialAppdoes not block back on pushed routes; it belongs on the route's own widget. - Forgetting to return a value. With
WillPopScope, the callback must return aFuture<bool>. Returning nothing or returningnullcauses a runtime error or lets the pop through. - Calling
Navigator.popinside the callback without a guard. This can loop: the pop triggers the callback again. Set a flag or usecanPopstate to break the cycle. - Assuming iOS behaves the same. iOS has no system back button; the swipe-back gesture is controlled by the navigator and is not blocked by
PopScopein the same way. Test on Android for Android behaviour. - Using
WillPopScopein a new project. It is deprecated in current Flutter versions. New code should usePopScopeto avoid warnings and future breakage. - Placing
PopScopeaboveScaffoldbut below another route. If the route was pushed withpushReplacementor the widget is rebuilt, state can reset. Keep the blocking state in aStatefulWidgetthat owns the route.
Tips
- Use
PopScopewithcanPop: falseandonPopInvokedWithResultfor new code; it is the current API and works with predictive back. - Keep the blocking logic minimal. Heavy work (network calls, database writes) inside the callback can delay the UI; show a loading indicator if needed.
- If you only want to block back on a specific condition, drive
canPopfrom state rather than returningfalseunconditionally. - Test with the Android emulator's back button and the gesture navigation setting, since both paths exist on modern devices.
- For a global "press back again to exit" pattern, handle it on the home route with a timestamp check, and let other routes pop normally.
Troubleshooting
- Back still pops the screen. Check that
PopScopewraps the route's widget and thatcanPopis actuallyfalseat the moment of the press. AsetStatethat flips it totrueearlier will let the pop through. - Callback never fires. Confirm you are on the route you think you are. If a nested navigator handles the pop, the outer
PopScopewill not see it. Move the widget inside the nested navigator's route. - Dialog appears twice. The callback can fire for both the button and the gesture. Guard with a boolean like
_isDialogShowingand reset it when the dialog closes. - Deprecation warning on
WillPopScope. Migrate toPopScope. The migration is mostly mechanical:onWillPopbecomesonPopInvokedWithResult, and the return value becomes thecanPopproperty. - App exits instead of showing the dialog. On the root route, the system may close the app if no route can handle the pop. Ensure your handler is on the route that is actually current, and avoid returning
falsewithout showing feedback.
Frequently Asked Questions
Can I disable the back button completely?
You can block the default pop with PopScope(canPop: false), but you cannot remove the system button itself. The user can still leave the app via the home button or recents; you are only intercepting the in-app back navigation.
What is the difference between WillPopScope and PopScope?
WillPopScope is the older API that returns a Future<bool> to allow or block a pop. PopScope is the newer replacement that uses a canPop boolean and an onPopInvokedWithResult callback, and it integrates with Android's predictive back gesture.
Does this work with the Android back gesture?
Yes, on Android 10 and later the predictive back gesture triggers the same pop handling, so PopScope intercepts it. Older WillPopScope also catches the gesture on most versions, but PopScope is the recommended choice.
Why does my back button still close the app?
If the current route has no PopScope blocking it, the system pops the route and, on the root route, exits the app. Place the widget on the route that is actually on top, and confirm canPop is false when the press happens.
Can I override back only when a form has unsaved changes?
Yes. Keep a boolean in state, set canPop: !_hasUnsavedChanges, and call setState when the form becomes dirty or is saved. That way back works normally when there is nothing to lose.
Does PopScope work on iOS?
It compiles and runs, but iOS has no system back button. The swipe-back gesture is managed by the navigator and may not be blocked the same way. Test on iOS separately if that platform matters to you.