1

I faced an issue with a a custom TextWatcher in Android 10.

The issue was that our custom TextWatcher was keeping a reference to the Editable passed in as a parameter of afterTextChanged.

This reference was compared to the new Editable every time afterTextChanged is called.

Apparently, on Android 10, the Editable instance is always the same across afterTextChanged invocations.

This was not the case before Android 10, hence the issue and this question.

The following is a simplification of the implementation

class MyTextWatcher : TextWatcher {

  private var oldEditable: Editable? = null

  override fun afterTextChanged(newEditable: Editable) {

    // if this is Android 10, then the following is always true
    // (except when oldEditable == null)

    // If android < 10, this is always false, as there's 
    // always a different instance of Editable being passed.

    if (oldEditable == newEditable) {

    }else{

      oldEditable = newEditable // Hold to the new instance

    }
  }

  override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
    ...
  }

  override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
    ...
  }
}

I understand the previous implementation might not be perfect, so I guess the questions are:

  • Was the initial implementation flawed and we should never have assumed that the instance will always be different?

  • Is the new behavior the expected and correct? or it is some sort of regression ?

  • Is there any place where I can find documentation about this? This kind of framework changes are tricky to detect and would love to be on top of them.

Thanks

Robert Estivill
  • 12,369
  • 8
  • 43
  • 64

0 Answers0