From 642f9f6e9ccc59a0433706d0e00a461936dc1ddc Mon Sep 17 00:00:00 2001 From: Adriana Maries Date: Wed, 20 Mar 2024 10:00:03 +0200 Subject: [PATCH] Bug 1812181 - Firefox for Android crashes submitting form Add a check for fragment attached and current context nullability before using requireContext. --- .../CreditCardSaveDialogFragment.kt | 37 ++++++++++++------- 1 file changed, 23 insertions(+), 14 deletions(-) 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.