安卓应用点击返回键触发Dialog时崩溃问题求助
Hey there, let's break down why your app crashes when pressing the back button and fix it up step by step!
What's Causing the Crash?
The root issue here is using getApplicationContext() to create your AlertDialog.Builder.
Dialogs in Android are tightly coupled to an Activity's window system—they need to be attached to a specific Activity context to render properly. The application-level ApplicationContext doesn't have an associated window, so when you call dialog.show(), the system can't find a valid window to display the dialog, which throws a WindowManager$BadTokenException (the crash you're seeing).
A minor side note: You're not calling the parent class's super.onBackPressed() in your override, but that's not the direct cause of the crash here. It's still good to keep lifecycle best practices in mind, though!
Fixes to Resolve the Crash
1. Use the Correct Context
Replace getApplicationContext() with your Activity instance (this), since your class is an Activity and already holds the valid window-bound context:
AlertDialog.Builder builder = new AlertDialog.Builder(this, android.R.style.Theme_DeviceDefault);
2. Clean Up the Exit Logic (Recommended)
Instead of using System.exit(1) (which force-kills the entire process, not the most Android-friendly approach), use finish() to properly close the Activity. If you need to exit the entire app, finishAffinity() will close all activities in your app's task stack:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public void onBackPressed() { AlertDialog.Builder builder = new AlertDialog.Builder(this, android.R.style.Theme_DeviceDefault); builder.setTitle("Exit") .setMessage("Do you really want to Exit ? ") .setPositiveButton("Yes", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { // Properly close the Activity finish(); // Uncomment below to exit the entire app // finishAffinity(); } }) .setNegativeButton("No", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { dialogInterface.dismiss(); } }); AlertDialog dialog = builder.create(); dialog.show(); }
3. Prevent Duplicate Dialogs (Optional Optimization)
If a user taps the back button quickly multiple times, your current code will create multiple overlapping dialogs. Add a check to avoid this with a member variable:
private AlertDialog exitDialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public void onBackPressed() { // Don't create a new dialog if one is already showing if (exitDialog != null && exitDialog.isShowing()) { return; } exitDialog = new AlertDialog.Builder(this, android.R.style.Theme_DeviceDefault) .setTitle("Exit") .setMessage("Do you really want to Exit ? ") .setPositiveButton("Yes", (dialog, which) -> finish()) .setNegativeButton("No", (dialog, which) -> dialog.dismiss()) .create(); exitDialog.show(); }
内容的提问来源于stack exchange,提问作者Pratham Khurana

