From eb9fc871414987174937d31b85854eebfec324af Mon Sep 17 00:00:00 2001 From: jinliu9508 Date: Wed, 30 Oct 2024 11:57:00 -0700 Subject: [PATCH] check activity availability before showing a dialog --- .../AlertDialogPrepromptForAndroidSettings.kt | 29 ++++++++++--------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/OneSignalSDK/onesignal/core/src/main/java/com/onesignal/core/internal/permissions/AlertDialogPrepromptForAndroidSettings.kt b/OneSignalSDK/onesignal/core/src/main/java/com/onesignal/core/internal/permissions/AlertDialogPrepromptForAndroidSettings.kt index 9f5028dbb..604221f56 100644 --- a/OneSignalSDK/onesignal/core/src/main/java/com/onesignal/core/internal/permissions/AlertDialogPrepromptForAndroidSettings.kt +++ b/OneSignalSDK/onesignal/core/src/main/java/com/onesignal/core/internal/permissions/AlertDialogPrepromptForAndroidSettings.kt @@ -56,18 +56,21 @@ object AlertDialogPrepromptForAndroidSettings { // simulate showing a dialog in a finishing activity activity.finish() - AlertDialog.Builder(activity) - .setTitle(title) - .setMessage(message) - .setPositiveButton(R.string.permission_not_available_open_settings_option) { dialog, which -> - callback.onAccept() - } - .setNegativeButton(android.R.string.no) { dialog, which -> - callback.onDecline() - } - .setOnCancelListener { - callback.onDecline() - } - .show() + // ensure the activity that will be showing the dialog is available + if (activity != null && !activity.isFinishing) { + AlertDialog.Builder(activity) + .setTitle(title) + .setMessage(message) + .setPositiveButton(R.string.permission_not_available_open_settings_option) { dialog, which -> + callback.onAccept() + } + .setNegativeButton(android.R.string.no) { dialog, which -> + callback.onDecline() + } + .setOnCancelListener { + callback.onDecline() + } + .show() + } } }