2

In my application i have a fragment with a EditText that implements a textWatcher to set a mask...It works fine Android before 27 (Oreo), but on Oreo i notice sometimes it not works properly...like it was called twice

Even if i just removeTextChangedListener, settext and addTextChangedListener again sometimes it repeats a character. If i erase a character it happens too.

etCPF.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        etCPF.removeTextChangedListener(this);
        etCPF.setText(charSequence);
        etCPF.setSelection(etCPF.getText().length());
        etCPF.addTextChangedListener(this);
     }

     @Override
     public void afterTextChanged(Editable editable) { }
});

xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <EditText
            android:id="@+id/editText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:inputType="textNoSuggestions" />

        <Button
            android:id="@+id/btnSearch"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:text="Search" />

    </LinearLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rvResult"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

Example: If i hit as on keyboard, on edittext randonly appear as or ass

In log screen i got this:

08-07 15:22:23.053 7158-7158/com.alelo.mobile.gestorauto.dev W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection
08-07 15:22:23.093 1850-1850/com.google.android.inputmethod.latin W/GInputConnectionWrapper: Fixing selection indices to 1, 1
08-07 15:22:23.119 1850-1850/com.google.android.inputmethod.latin I/native: input-context-store.cc:113 Using roll back input context operation: [AbortComposing] reason: client [1219] vs decoder [1220]
08-07 15:22:23.897 7158-7158/com.alelo.mobile.gestorauto.dev W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection
08-07 15:22:23.898 1850-1850/com.google.android.inputmethod.latin I/native: input-context-store.cc:113 Using roll back input context operation: [AbortComposing] reason: client [1226] vs decoder [1227]
08-07 15:22:23.907 7158-7158/com.alelo.mobile.gestorauto.dev W/IInputConnectionWrapper: beginBatchEdit on inactive InputConnection
getTextBeforeCursor on inactive InputConnection
getTextAfterCursor on inactive InputConnection
08-07 15:22:23.910 7158-7158/com.alelo.mobile.gestorauto.dev W/IInputConnectionWrapper: getSelectedText on inactive InputConnection
08-07 15:22:23.912 7158-7158/com.alelo.mobile.gestorauto.dev W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection
08-07 15:22:23.913 7158-7158/com.alelo.mobile.gestorauto.dev W/IInputConnectionWrapper: beginBatchEdit on inactive InputConnection
getTextBeforeCursor on inactive InputConnection
08-07 15:22:23.915 7158-7158/com.alelo.mobile.gestorauto.dev W/IInputConnectionWrapper: getTextAfterCursor on inactive InputConnection
08-07 15:22:23.916 7158-7158/com.alelo.mobile.gestorauto.dev W/IInputConnectionWrapper: getSelectedText on inactive InputConnection
08-07 15:22:23.917 7158-7158/com.alelo.mobile.gestorauto.dev W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection
beginBatchEdit on inactive InputConnection
getTextBeforeCursor on inactive InputConnection
08-07 15:22:23.918 7158-7158/com.alelo.mobile.gestorauto.dev W/IInputConnectionWrapper: getTextAfterCursor on inactive InputConnection
getSelectedText on inactive InputConnection
08-07 15:22:23.919 1850-1850/com.google.android.inputmethod.latin I/native: input-context-store.cc:113 Using roll back input context operation: [AbortComposing] reason: client [1231] vs decoder [1232]
08-07 15:22:23.920 7158-7158/com.alelo.mobile.gestorauto.dev W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection
08-07 15:22:23.922 7158-7158/com.alelo.mobile.gestorauto.dev W/IInputConnectionWrapper: beginBatchEdit on inactive InputConnection
08-07 15:22:23.923 7158-7158/com.alelo.mobile.gestorauto.dev W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
getTextAfterCursor on inactive InputConnection
getSelectedText on inactive InputConnection
endBatchEdit on inactive InputConnection
08-07 15:22:23.926 7158-7158/com.alelo.mobile.gestorauto.dev W/IInputConnectionWrapper: beginBatchEdit on inactive InputConnection
getTextBeforeCursor on inactive InputConnection
getTextAfterCursor on inactive InputConnection
getSelectedText on inactive InputConnection
08-07 15:22:23.927 7158-7158/com.alelo.mobile.gestorauto.dev W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection
08-07 15:22:23.930 1850-1850/com.google.android.inputmethod.latin I/native: input-context-store.cc:113 Using roll back input context operation: [AbortComposing] reason: client [1235] vs decoder [1236]
08-07 15:22:23.949 1850-1850/com.google.android.inputmethod.latin W/GInputConnectionWrapper: Fixing selection indices to 3, 3

Anyone had this issue before?Anyone had this issue before?

  • 1
    Could you share xml part of your editText? – H. Ekici Aug 07 '18 at 18:38
  • Can try it after removing inputType? – H. Ekici Aug 07 '18 at 19:00
  • If you are `onCreate` or other method than repeat when activity need rebuild what I think is because you add more than one `TextWacter` and other can't be removed because you have not the ref so what I suggest you is to put you `TextWatcher` in activity and add it when `etCPF` null you find it and add watcher. – Crammeur Aug 07 '18 at 20:26
  • And if you want example I will need more code `Activity` or `Fragment` to help you – Crammeur Aug 07 '18 at 20:59
  • i tested it on a Motorola device and nothing happened...but on Samsung devices and on android emulator this problem happens. – Rafael Godinho Brandão Aug 13 '18 at 18:28

1 Answers1

0

This what a little example of what I would said in comment.

Example

if (etCPF == nul) {
    etCPF = (EditText) findViewById(R.id.editText)
    etCPF.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            etCPF.removeTextChangedListener(this);
            etCPF.setText(charSequence);
            etCPF.setSelection(etCPF.getText().length());
            etCPF.addTextChangedListener(this);
         }

         @Override
         public void afterTextChanged(Editable editable) { }
    });
}

Why this is because I think we are in lifecycle method than is repeated when Activity by example rotation and then you add many time TextWatcher. So this can explain why they many TextWatcher

Crammeur
  • 678
  • 7
  • 17