afterTextChanged() callback being called without the text being actually changed

afterTextChanged() callback being called without the text being actually changed


up vote8down votefavorite

I have a fragment with an EditText and inside the onCreateView() I add a TextWatcher to the EditText.

Each time the fragment is being added for the second time afterTextChanged(Editable s)callback is being called without the text ever being changed.

Here is a code snippet :

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
...
    myEditText = (EditText) v.findViewById(R.id.edit_text);
    myEditText.addTextChangedListener(textWatcher);
...
}

TextWatcher textWatcher = new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        searchProgressBar.setVisibility(View.INVISIBLE);
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        Log.d(TAG, "after text changed");
    }
}

I also set the fragment to retain its state, and I keep the instance of the fragment in the activity.

android android-edittext


shareimprove this question

edited Sep 17 ‘13 at 15:28

asked Dec 5 ‘12 at 10:27

meh

11.3k52950

 

    

Each time the fragment is being added for the second time - can you share some code related to this? – Luksprog Dec 5 ‘12 at 12:18
    

It is a very common thing to do when switching fragments. transaction.replace(r.id.container, fragment); transaction.commit(); nothing special, I have solved the problem with adding a flag I will be posting a solution. – meh Dec 5 ‘12 at 12:24

add a comment

1 Answer

activeoldestvotes


up vote20down voteaccepted

Edited solution:

As it seems the text was changed from the second time the fragment was attached because the fragment restored the previous state of the views.

My solution was adding the text watcher in the onResume() since the state was restoredbefore the onResume was called.

@Override
public void onResume() {
    super.onResume();
    myEditText.addTextChangedListener(textWatcher);
}

shareimprove this answer

edited Mar 13 ‘15 at 18:23

answered Dec 5 ‘12 at 12:34

meh

11.3k52950

 

    

This solution worked for me very well! – GFPF Mar 13 ‘15 at 18:39
    

Great, that fixed it for me! – Bart Bergmans Jul 23 ‘15 at 6:51
    

Thank you. This was driving me absolutely crazy. – cohenadair Jan 24 at 5:49

时间: 2024-12-10 22:52:41

afterTextChanged() callback being called without the text being actually changed的相关文章

谷歌,火狐提示来自"http://xxx.com/file"的资源已被阻止,因为 MIME 类型("text/plain")不匹配(X-Content-Type-Options: nosniff),如何解决?

在使用ueditor编辑的过程中无法上传图片,谷歌火狐浏览器提示Cross-Origin Read Blocking (CORB) blocked cross-origin response http://XXX?action=config&callback=bd__editor__dkhbuv with MIME type text/plain,怎么解决?查看该文件请求,发现服务器返回头中多了这么个玩意 X-Content-Type-Options: nosniff,这个是什么意思呢? 查阅资

异步加载text资源,加载一次、执行一次、链式回调

function getText(time,callback) { setTimeout(function () { callback(time) },time) } var callbackMap={};//记录加载完成的回调 var resMap={};//记录url var loadbackMap={};//记录url 加载完成 var loadedMap={};//记录url 加载完成 var loadedI=-1;//记录url 加载完成位置 var orderI=0;//记录url

android中RadioGroup、RadioButton、Spinner、EditText用法详解(含示例截图和源代码)

为了保护版权.尊重原创,转载请注明出处:http://blog.csdn.net/u013149325/article/details/43237757,谢谢! 今天在项目中用到了android中常用的RadioGroup.RadioButton.Spinner.EditText等控件,在此介绍一下它们的用法,希望对需要的朋友有帮助. 一.RadioGroup和RadioButton的使用 RadioButton就是我们常见的单选按钮,一个RadioGroup可以包含多个单选按钮,但是每次只能选

android之 TextWatcher的监听

以前用过android.text.TextWatcher来监听文本发生变化,但没有仔细去想它,今天兴致来了就发个疯来玩玩吧! 有点担心自己理解错,所以还是先把英文API解释给大家看看 1.什么情况下使用了? When an object of a type is attached to an Editable, its methods will be called when the text is changed. 2.下面是它的三个抽象方法 /** *This method is called

我的Android进阶之旅------>Android关于TextWatcher的初步了解

首先来看一下TextWatcher的源代码 package android.text; /** * When an object of a type is attached to an Editable, its methods will * be called when the text is changed. */ public interface TextWatcher extends NoCopySpan { /** * This method is called to notify y

解决跨域问题

什么是同源策略? 同源策略是浏览器的一项最为基本同时也是必须遵守的安全策略.同源策略的存在,限制了"源"自A的脚本只能操作"同源"页面的DOM,"跨源"操作来源于B的页面将会被拒绝.所谓的"同源",必须要求相应的URI在如下3个方面均是相同的. 主机名称(域名/子域名或者IP地址) 端口号 网络协议(Scheme,分别采用"http"和"https"协议的两个URI被视为不同源 对于一段

JSONP的诞生、原理及应用实例

问题: 页面中有一个按钮,点击之后会更新网页中的一个盒子的内容. Ajax可以很容易的满足这种无须刷新整个页面就可以实现数据变换的需求. 但是,Ajax有一个缺点,就是他不允许跨域请求资源. 如果我的代码在codepen上,我不能将我的数据放到codepen网站上,那么我只能放到我自己的服务器中,这样的话,就无法通过Ajax访问到这个数据了. 解决: 想要实现这种跨域资源请求,有很多解决办法,列举出一部分: 让服务器来加载远程数据,然后在用户请求时提供给浏览器. 用<script>或是<

面向对象封装分页代码

首先 看下关键的api, var DEFAULTS = { defaultIndex: 1, //默认页 defaultSize: 10, //默认分页大小 pageIndexName: 'page', //分页参数名称 pageSizeName: 'page_size', //分页大小参数名称 onChange: '', //分页改变或分页大小改变时的回调 onInit: '', //初始化完毕的回调 allowActiveClick: true, //控制当前页是否允许重复点击刷新 midd

无刷新分页技术,Ajax+smarty

这里运用Smarty模板,更简单 本文主要的技术:AJAX,PHP,Smarty,另外自己封装了一个很简单的类 类: (function(){    function $(id) {        return document.getElementById(id);    }    $.init=function() {        try{return new XMLHttpRequest();}catch(e){};        try{return new ActiveXObject