Flutter中如何重写系统返回按钮实现退出确认弹窗?
Got it! Let's solve this problem step by step—whether you're working with Android native or Flutter, here's how to intercept the system back button and show that "Do you want to exit the App?" confirmation dialog on your Home widget/page.
处理系统返回按钮弹出退出确认对话框
方案一:Android原生开发(Home Activity/Fragment)
1. 如果你的Home是一个Activity
The core way to handle the back button in Android is overriding the onBackPressed() method in your Home Activity. Here's exactly how to implement the dialog:
@Override public void onBackPressed() { // Build the confirmation dialog AlertDialog exitConfirmationDialog = new AlertDialog.Builder(this) .setTitle("Exit App?") .setMessage("Do you want to exit the App?") .setPositiveButton("Yes", (dialog, which) -> { // User confirmed exit—close all related activities to fully exit the app finishAffinity(); }) .setNegativeButton("No", (dialog, which) -> { // User canceled, just dismiss the dialog dialog.dismiss(); }) .setCancelable(false) // Prevent closing the dialog by tapping outside .create(); exitConfirmationDialog.show(); }
2. 如果你的Home是一个Fragment
If your Home UI is a Fragment, it's better to use OnBackPressedDispatcher to register a callback instead of hardcoding logic in the Activity:
@Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); // Register a back button callback requireActivity().getOnBackPressedDispatcher().addCallback( getViewLifecycleOwner(), new OnBackPressedCallback(true) { @Override public void handleOnBackPressed() { showExitDialog(); } } ); } private void showExitDialog() { new AlertDialog.Builder(requireContext()) .setMessage("Do you want to exit the App?") .setPositiveButton("Yes", (dialog, which) -> { requireActivity().finishAffinity(); this.remove(); // Remove the callback to avoid duplicate triggers }) .setNegativeButton("No", (dialog, which) -> dialog.dismiss()) .show(); }
方案二:Flutter开发(Home Page/Widget)
In Flutter, you'll use the WillPopScope widget to wrap your Home content and intercept the back button event:
class HomeWidget extends StatelessWidget { @override Widget build(BuildContext context) { return WillPopScope( // This callback runs when the system back button is pressed onWillPop: () async { // Show the confirmation dialog final bool? shouldExit = await showDialog( context: context, builder: (context) => AlertDialog( title: const Text("Confirm Exit"), content: const Text("Do you want to exit the App?"), actions: [ TextButton( onPressed: () => Navigator.of(context).pop(false), child: const Text("No"), ), TextButton( onPressed: () => Navigator.of(context).pop(true), child: const Text("Yes"), ), ], ), ); // Return true to allow exit, false to block it return shouldExit ?? false; }, child: const Scaffold( appBar: AppBar(title: Text("Home")), body: Center(child: Text("Your Home Widget Content")), ), ); } }
Quick Notes
- For Android,
finishAffinity()closes all activities associated with your app, which is better thansuper.onBackPressed()if you want to fully exit the app instead of just going back to the previous screen. - In Flutter,
onWillPopreturns aFuture<bool>—the dialog's result determines whether the back navigation is allowed. - Setting
setCancelable(false)(Android) or ensuring the user must tap Yes/No prevents accidental exits when tapping outside the dialog.
内容的提问来源于stack exchange,提问作者Tushar Pol
相关产品推荐
相关产品推荐

