diff --git a/android-components/components/feature/prompts/src/main/java/mozilla/components/feature/prompts/creditcard/CreditCardSaveDialogFragment.kt b/android-components/components/feature/prompts/src/main/java/mozilla/components/feature/prompts/creditcard/CreditCardSaveDialogFragment.kt index 428c48fc663e..a0ff02bfea7c 100644 --- a/android-components/components/feature/prompts/src/main/java/mozilla/components/feature/prompts/creditcard/CreditCardSaveDialogFragment.kt +++ b/android-components/components/feature/prompts/src/main/java/mozilla/components/feature/prompts/creditcard/CreditCardSaveDialogFragment.kt @@ -5,6 +5,7 @@ package mozilla.components.feature.prompts.creditcard import android.app.Dialog +import android.content.Context import android.content.DialogInterface import android.os.Bundle import android.view.LayoutInflater @@ -137,23 +138,31 @@ internal class CreditCardSaveDialogFragment : PromptDialogFragment() { confirmResult = validationDelegate.shouldCreateOrUpdate(creditCard) withContext(Main) { - when (confirmResult) { - is Result.CanBeCreated -> setViewText( - view = view, - header = requireContext().getString(R.string.mozac_feature_prompts_save_credit_card_prompt_title), - cancelButtonText = requireContext().getString(R.string.mozac_feature_prompt_not_now), - confirmButtonText = requireContext().getString(R.string.mozac_feature_prompt_save_confirmation), - ) - is Result.CanBeUpdated -> setViewText( - view = view, - header = requireContext().getString(R.string.mozac_feature_prompts_update_credit_card_prompt_title), - cancelButtonText = requireContext().getString(R.string.mozac_feature_prompts_cancel), - confirmButtonText = requireContext().getString(R.string.mozac_feature_prompt_update_confirmation), - showMessageBody = false, - ) + checkIfFragmentAttached { + when (confirmResult) { + is Result.CanBeCreated -> setViewText( + view = view, + header = requireContext().getString(R.string.mozac_feature_prompts_save_credit_card_prompt_title), + cancelButtonText = requireContext().getString(R.string.mozac_feature_prompt_not_now), + confirmButtonText = requireContext().getString(R.string.mozac_feature_prompt_save_confirmation), + ) + + is Result.CanBeUpdated -> setViewText( + view = view, + header = requireContext().getString(R.string.mozac_feature_prompts_update_credit_card_prompt_title), + cancelButtonText = requireContext().getString(R.string.mozac_feature_prompts_cancel), + confirmButtonText = requireContext().getString(R.string.mozac_feature_prompt_update_confirmation), + showMessageBody = false, + ) + } } } } + private fun checkIfFragmentAttached(operation: Context.() -> Unit) { + if (isAdded && context != null) { + operation(requireContext()) + } + } /** * Updates the header and button text in the dialog.