splitwise / TokenAutoComplete

Gmail style MultiAutoCompleteTextView for Android
Apache License 2.0
1.3k stars 383 forks source link

onTokenAdded and onTokenRemoved not firing on initial population of fragment #234

Closed ntyrrell closed 8 years ago

ntyrrell commented 8 years ago

Good morning,

I'm loving this widget thus far, although I am coming up against some unusual behaviour that I'm not fully understanding.

I have two views in a fragment that hold tags (in my case, skills and interests). I also wanted to add the functionality of adding new tags on hitting the return key as well as the delimiting characters. These are then saved to the local database after clicking a save button. The code below is added during the onCreateView method of the fragment.

etSkills.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_NEXT) {
            final TagCompletionView tagCompletionView = (TagCompletionView) v;
            tagCompletionView.performCompletion();
            return true;
        }
        return false;
    }
});
etSkills.setTokenListener(new TokenCompleteTextView.TokenListener<String>() {
    @Override
    public void onTokenAdded(String token) {
        System.out.println("New item entered: "+token);
        UserSkills skill = new UserSkills();
        skill.Skill = token;
        mSkills.add(skill);
    }
    @Override
    public void onTokenRemoved(String token) {
        System.out.println("Item removed: "+token);
        int index = -1;
        for (int i = 0; i < mSkills.size(); i++) {
            if (mSkills.get(i).Skill.equals(token)) {
                index = i;
            }
        }
        if (index != -1) {
            mSkills.remove(index);
        }
    }
});

However, having gone through the debugger, it appears that onTokenAdded and onTokenRemoved do not fire if it is the first time that the fragment is viewed. On the other hand, if the application is hidden from view (i.e. the phone goes into standby mode or another application is brought up), these methods will fire as expected.

Any advice/guidance as to why these views are behaving this way would be greatly appreciated.

Kind regards,

Nicholas

mgod commented 8 years ago

Rather than setting your own OnEditorActionListener , override onEditorAction in your TokenCompleteTextView subclass – most likely your listener is getting removed by the one in the library and there's some order that changes when the activity resumes.

ntyrrell commented 8 years ago

Hi,

Thank you for the response. However, I've moved the listener over to the subclass, and still no luck.

It doesn't appear to be an issue with OnEditorActionListener. Any other ideas as to what could be causing the issue?