android - onTextChanged() method called without calling setText() or user's typing -
textwatcher
works fine until activity destroyed , before restoring old saved bundle. when restore saved bundle ontextchanged()
called both of edittexts. happens if don't restore values (don't call settext()). note user not interacting in way.
code :
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.create_trip); log.d(tag, "oncreate"); fromlocation = (edittext) findviewbyid(r.id.from_location); tolocation = (edittext) findviewbyid(r.id.to_location); // set edittexts listeners setfromlocationedittextlisteres(); settolocationedittextlisteres(); // check whether we're recreating destroyed instance if (savedinstancestate != null) { log.d(tag, "restoring state saved bundle"); ... } else { log.e(tag, "setting default values"); ... } } // set listener fromlocation edittext private void setfromlocationedittextlisteres() { fromlocation.setonfocuschangelistener(new onfocuschangelistener() { @override public void onfocuschange(view v, boolean hasfocus) { if (hasfocus) { log.d(tag, "fromlocation gained focus."); } } }); fromlocation.addtextchangedlistener(new textwatcher() { @override public void aftertextchanged(editable s) { log.e(tag, "fromlocation after."); } @override public void beforetextchanged(charsequence s, int start, int count, int after) { } @override public void ontextchanged(charsequence s, int start, int before, int count) { if (fromlocation == null) { log.d(tag, "fromlocation null................"); } if (count != before) { edittextvaluechanged(fromlocation); } } }); fromlocation.setonkeylistener(new view.onkeylistener() { public boolean onkey(view v, int keycode, keyevent event) { // if user pressed enter if (keycode == keyevent.keycode_enter) { } // if pressed other enter key let others handle // event return false; } }); } // set listener tolocation edittext private void settolocationedittextlisteres() { tolocation.setonfocuschangelistener(new onfocuschangelistener() { @override public void onfocuschange(view v, boolean hasfocus) { if (hasfocus) { log.d(tag, "tolocation gained focus."); } } }); tolocation.addtextchangedlistener(new textwatcher() { @override public void aftertextchanged(editable s) { log.e(tag, "tolocation after."); } @override public void beforetextchanged(charsequence s, int start, int count, int after) { } @override public void ontextchanged(charsequence s, int start, int before, int count) { if (count != before) { edittextvaluechanged(tolocation); } } }); tolocation.setonkeylistener(new view.onkeylistener() { public boolean onkey(view v, int keycode, keyevent event) { // if user pressed enter if (keycode == keyevent.keycode_enter) { } // if pressed other enter key let others handle // event return false; } }); } @override public void onsaveinstancestate(bundle savedinstancestate) { log.e(tag, "onsaveinstancestate called."); // save activity state savedinstancestate.putcharsequence(from_location_text, fromlocation.gettext()); savedinstancestate.putcharsequence(to_location_text, tolocation.gettext()); super.onsaveinstancestate(savedinstancestate); }
after headache of few hours reason of issue comes out following code :
@override public void onsaveinstancestate(bundle savedinstancestate) { log.e(tag, "onsaveinstancestate called."); // save activity state savedinstancestate.putcharsequence(from_location_text, fromlocation.gettext()); savedinstancestate.putcharsequence(to_location_text, tolocation.gettext()); super.onsaveinstancestate(savedinstancestate); }
what happens android stores edittext
's state (if id
attribute specified in layout file) , when activity restored using saved bundle restores edittext's value calling myedittext.settext()
. triggering call text changed listeners.
solution removing lines above code saves edittext
's text. not save edittext
or similar view
's state explicitly. let android you.
Comments
Post a Comment