From 7281f4f923e052dba7e973c93dc01bc92374d95d Mon Sep 17 00:00:00 2001 From: Quang Luong Date: Wed, 29 Jun 2022 18:13:35 +0000 Subject: Cancel all dialogs in onStop() if not a configuration change When the configuration changes, we do not cancel any dialogs so that we can recreate them when the Activity is restarted. However, if onStop() is called without a configuration change (different Activity coming to the foreground), then we should cancel all dialogs. Bug: 237482092 Test: Launch dialog with `adb shell cmd wifi launch-dialog-simple -t Title -m Message -c 0`, then run `adb shell am start -n com.google.android.setupwizard/.SetupWizardTestActivity` to launch SetupWizard, and verify the dialog was cancelled. Verify launching dialog and toggling dark mode does not cancel the dialog. Change-Id: I1eff91d5d81098ddaa00fe3712987eadc808e00d --- .../android/wifi/dialog/WifiDialogActivity.java | 29 ++++++++++++++++------ 1 file changed, 21 insertions(+), 8 deletions(-) (limited to 'WifiDialog') diff --git a/WifiDialog/src/com/android/wifi/dialog/WifiDialogActivity.java b/WifiDialog/src/com/android/wifi/dialog/WifiDialogActivity.java index 5fa0177296..01de19e200 100644 --- a/WifiDialog/src/com/android/wifi/dialog/WifiDialogActivity.java +++ b/WifiDialog/src/com/android/wifi/dialog/WifiDialogActivity.java @@ -237,13 +237,26 @@ public class WifiDialogActivity extends Activity { protected void onStop() { super.onStop(); unregisterReceiver(mCloseSystemDialogsReceiver); - // Dismiss and remove any active Dialogs to prevent window leaking. - for (int i = 0; i < mActiveDialogsPerId.size(); i++) { - Dialog dialog = mActiveDialogsPerId.valueAt(i); - dialog.setOnDismissListener(null); - dialog.dismiss(); + + if (isChangingConfigurations()) { + // If we're stopping due to a configuration change, dismiss all the dialogs without + // removing it from mLaunchIntentsPerId to prevent window leaking. The dialogs will be + // recreated from mLaunchIntentsPerId in onStart(). + for (int i = 0; i < mActiveDialogsPerId.size(); i++) { + Dialog dialog = mActiveDialogsPerId.valueAt(i); + // Set the dismiss listener to null to prevent removing the Intent from + // mLaunchIntentsPerId. + dialog.setOnDismissListener(null); + dialog.dismiss(); + } + mActiveDialogsPerId.clear(); + } else { + // If we're stopping because we're switching to a new Activity, remove and cancel all + // the dialogs. + while (mActiveDialogsPerId.size() > 0) { + removeIntentAndPossiblyFinish(mActiveDialogsPerId.keyAt(0)); + } } - mActiveDialogsPerId.clear(); } @Override @@ -257,7 +270,7 @@ public class WifiDialogActivity extends Activity { } /** - * Remove the Intent and corresponding dialog of the given dialogId (dismissing it if it is + * Remove the Intent and corresponding dialog of the given dialogId (cancelling it if it is * showing) and finish the Activity if there are no dialogs left to show. */ private void removeIntentAndPossiblyFinish(int dialogId) { @@ -265,7 +278,7 @@ public class WifiDialogActivity extends Activity { Dialog dialog = mActiveDialogsPerId.get(dialogId); mActiveDialogsPerId.remove(dialogId); if (dialog != null && dialog.isShowing()) { - dialog.dismiss(); + dialog.cancel(); } if (mIsVerboseLoggingEnabled) { Log.v(TAG, "Dialog id " + dialogId + " removed."); -- cgit v1.2.3-59-g8ed1b