I'm developing an Android App for a client that mostly uses a NOMU T20 device with a Honeywell scanner integrated.

I'm working with a virtual keyboard adding a TextWatcher to an EditText that displays de barcode scanned and triggers a call to an API endpoint (using and end-character "\n"). It works fine if I do not navigate to another activity or fragment.

The issue is once I finish the current activity (aka "Act1") and navigates to another one (Aka "Act2") with another EditText with its own TextWatcher and scan again it triggers both TextWatcher, first the one from "Act1" and last from "Act2". That means I have an extra call to the 1st API endpoint when I just want to call the 2nd API endpoint for Act2 (the current activity).

This issue can be reproduced too with fragments.

I tried to use this library that is using a broadcast receiver -> https://github.com/hjgode/IntentAPISample_by_HW without success, I'm not getting any data.

Code to reproduce the issue (only with an integrated scanner not an external)

Add a TextChangedListener on EditText from activities:

edit.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {

            if (s.toString().trim().length() == 0)
                return;

            if (s.length() > 0) {

                char lastCharacter = s.charAt(s.length() - 1);

                if (lastCharacter == '\n' || lastCharacter == '\r') {
                    String barcode = s.subSequence(0, s.length() - 1).toString();
                    edit.setText("");                       
                    Toast.makeText(MainActivity.this, barcode, Toast.LENGTH_LONG).show();
                    Log.e("MainActivity", barcode);
                }
            }
        }
    });

On Act2 when scanning I got this log in the terminal. I tried to figure out the "I/song" and "I/song-view" messages.

    I/song: hava scan message androidx.appcompat.widget.AppCompatEditText{9895392 VFED..CL. .F....ID 206,467-515,558 #7f080097 app:id/edit_barcode}
I/song-view: hava scan message 0
E/MainActivity: HORAE
I/song: hava scan message androidx.appcompat.widget.AppCompatEditText{fc384f3 VFED..CL. .F...... 206,467-515,558 #7f080098 app:id/edit_barcode2}
I/song-view: hava scan message 0
E/Act2: HORAE
D/WindowClient: Add to mViews: android.widget.LinearLayout{9fd1546 V.E...... ......I. 0,0-0,0}, this = android.view.WindowManagerGlobal@ff7017e
D/ViewRootImpl[Toast]: hardware acceleration = true , fakeHwAccelerated = false, sRendererDisabled = false, forceHwAccelerated = false, sSystemRendererDisabled = false
D/Surface: Surface::connect(this=0x8d7d9000,api=1)
D/Surface: Surface::allocateBuffers(this=0x8d7d9000)
D/WindowClient: Add to mViews: android.widget.LinearLayout{1653fd2 V.E...... ......I. 0,0-0,0}, this = android.view.WindowManagerGlobal@ff7017e
D/ViewRootImpl[Toast]: hardware acceleration = true , fakeHwAccelerated = false, sRendererDisabled = false, forceHwAccelerated = false, sSystemRendererDisabled = false
D/Surface: Surface::allocateBuffers(this=0x8bf1c000)
D/Surface: Surface::connect(this=0x8bf1c000,api=1)

If someone knows any way to solve this issue I'd appreciate it Thanks in advance

1

There are 1 answers

0
Zeronne On

I solved it by removing the TextWatcher on 'onStop/onDestroy' lifecycle method.

STEP 1: Implements my own TextWatcher -> MyTextWatcher

STEP 2: Instantiate MyTextWatcher on both Activities and attach it to an EditText

addTextChangedListener(mTextWatcherObj)

STEP 3: Remove the listener

removeTextChangedListener(mTextWatcherObj);

Thanks