TechBuzzPro

Flutter: How to Disable or Override the Android Back Button

· 6 min read · Phones & Tablets

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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 Navigator and widget build methods.
  • If you are on a recent Flutter version, use PopScope. WillPopScope is deprecated in newer releases but still works in older projects.

Step-by-Step

  1. 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.
  2. Choose your widget. For Flutter versions that support it, use PopScope. If your project is pinned to an older Flutter release, use WillPopScope. Do not mix them on the same route.
  3. Wrap the screen's top-level widget. In the build method, return PopScope(canPop: false, onPopInvokedWithResult: (didPop, result) { ... }, child: Scaffold(...)). The child is your existing widget tree. With WillPopScope, the equivalent is WillPopScope(onWillPop: () async { return false; }, child: Scaffold(...)).
  4. Decide what happens on back. Returning false (or setting canPop: 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 with Navigator.pushReplacement. If you want to allow the pop conditionally, return true or set canPop based on a state variable.
  5. Handle the confirmation dialog case. A common pattern is: user presses back, you show an AlertDialog asking "Discard changes?", and if they confirm you call Navigator.of(context).pop() yourself. With PopScope, note that calling pop inside the callback can re-trigger the callback; guard it with a boolean flag or use Navigator.pop after setting canPop to true via setState.
  6. 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.
  7. Check nested navigators. If your app uses a nested Navigator (for example inside a tab), the PopScope must be placed inside the route that the nested navigator manages. A PopScope on the outer route will not intercept pops handled by the inner navigator.
  8. Remove or relax the block when appropriate. If the block is only needed while a form is dirty, store a bool _hasUnsavedChanges and set canPop: !_hasUnsavedChanges. When the user saves, call setState so the next back press passes through.

Common Mistakes

  • Wrapping the wrong widget. PopScope and WillPopScope only affect routes they are part of. Putting it around MaterialApp does 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 a Future<bool>. Returning nothing or returning null causes a runtime error or lets the pop through.
  • Calling Navigator.pop inside the callback without a guard. This can loop: the pop triggers the callback again. Set a flag or use canPop state 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 PopScope in the same way. Test on Android for Android behaviour.
  • Using WillPopScope in a new project. It is deprecated in current Flutter versions. New code should use PopScope to avoid warnings and future breakage.
  • Placing PopScope above Scaffold but below another route. If the route was pushed with pushReplacement or the widget is rebuilt, state can reset. Keep the blocking state in a StatefulWidget that owns the route.

Tips

  • Use PopScope with canPop: false and onPopInvokedWithResult for 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 canPop from state rather than returning false unconditionally.
  • 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 PopScope wraps the route's widget and that canPop is actually false at the moment of the press. A setState that flips it to true earlier 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 PopScope will 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 _isDialogShowing and reset it when the dialog closes.
  • Deprecation warning on WillPopScope. Migrate to PopScope. The migration is mostly mechanical: onWillPop becomes onPopInvokedWithResult, and the return value becomes the canPop property.
  • 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 false without 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.