Search code examples
androidandroid-edittextonfocus

OnFocusChangeListener locks gui


I have an editText in my application and I'm adding a listener like in code below

if(edit!=null){
        edit.setOnFocusChangeListener(new View.OnFocusChangeListener() {            
            public void onFocusChange(View v, boolean hasFocus) {           
                // TODO Auto-generated method stub
                if( paramKey!=null){

                    if(!hasFocus){
                    if(edit.getText()!=null)
                        dataModel.updateParamsValue(paramKey, edit.getText().toString());
                    }

                }
            }

        });
    }

When I focus to my editText and enter a text it locks gui, do I miss something?


Solution

  • Is dataModel.updateParamsValue a heavy operation? Your listener works in the UI thread, so it will lock the UI till it completes. Try changing code like this:

    if(edit.getText()!=null)
                    v.post(new Runnable(){
                        dataModel.updateParamsValue(paramKey, edit.getText().toString());
                    });
                }