From ea0630b0bae579df578fc3cb21a85fcd57110074 Mon Sep 17 00:00:00 2001 From: Till Hellmund Date: Wed, 14 Sep 2022 18:24:06 -0400 Subject: [PATCH] Add check if text watcher is already running --- .../java/com/stripe/android/view/StripeEditText.kt | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/payments-core/src/main/java/com/stripe/android/view/StripeEditText.kt b/payments-core/src/main/java/com/stripe/android/view/StripeEditText.kt index ac7a47be205..3495062ef27 100644 --- a/payments-core/src/main/java/com/stripe/android/view/StripeEditText.kt +++ b/payments-core/src/main/java/com/stripe/android/view/StripeEditText.kt @@ -115,6 +115,12 @@ open class StripeEditText @JvmOverloads constructor( val internalFocusChangeListeners = mutableListOf() private var externalFocusChangeListener: OnFocusChangeListener? = null + private val isLastKeyDeleteTextWatcher = object : StripeTextWatcher() { + override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) { + isLastKeyDelete = count == 0 + } + } + protected open val accessibilityText: String? = null override fun setTextColor(colors: ColorStateList?) { @@ -199,14 +205,10 @@ open class StripeEditText @JvmOverloads constructor( // On some devices, the OnKeyListener isn't invoked for software keyboards. It is invoked on // other devices such as my Pixel. To fix the issue for all devices, we're adding an // additional text watcher to keep isLastKeyDelete in the correct state. - val textWatcher = object : StripeTextWatcher() { - override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) { - isLastKeyDelete = count == 0 - } + if (isLastKeyDeleteTextWatcher !in textWatchers.orEmpty()) { + addTextChangedListener(isLastKeyDeleteTextWatcher) } - addTextChangedListener(textWatcher) - // This method works for hard keyboards and older phones. setOnKeyListener { _, keyCode, event -> isLastKeyDelete = isDeleteKey(keyCode, event)