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.

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

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);
}
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

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

  1. android API之android.text.TextWatcher

    When an object of a type is attached to an Editable, its methods will be called when the text is cha ...

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

    在使用ueditor编辑的过程中无法上传图片,谷歌火狐浏览器提示Cross-Origin Read Blocking (CORB) blocked cross-origin response http ...

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

    首先来看一下TextWatcher的源码 package android.text; /** * When an object of a type is attached to an Editable ...

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

    首先来看一下TextWatcher的源代码 package android.text; /** * When an object of a type is attached to an Editabl ...

  5. Winform下CefSharp的引用、配置、实例与报错排除(源码)

    Winform下CefSharp的引用.配置.实例与报错排除 本文详细介绍了CefSharp在vs2013..net4.0环境下,创建Winfrom项目.引用CefSharp的方法,演示了winfro ...

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

    问题: 页面中有一个按钮,点击之后会更新网页中的一个盒子的内容. Ajax可以很容易的满足这种无须刷新整个页面就可以实现数据变换的需求. 但是,Ajax有一个缺点,就是他不允许跨域请求资源. 如果我的 ...

  7. jQuery动画与特效详解

    本文主要是讲解和学习jQuery的自动显隐,渐入渐出等. 1.显示和隐藏hide()和show() 对于动画来说,显示和隐藏是最基本的效果之一,本节简单介绍jQuery的显示和隐藏. 代码如下: &l ...

  8. jquery基础

    show() hide() toggle()         fadeIn() fadeOut() fadeToggle() fadeTo()         slideUp() slideDown( ...

  9. 前端之ajax

    前端之ajax 本节内容 ajax介绍 原生js实现ajax jquery实现ajax json 跨域请求 1. ajax介绍 AJAX(Asynchronous Javascript And XML ...

随机推荐

  1. ASP.NET中数据邦定效率问题的一点看法 - 转载(自由的天空)

    在 做Asp.NET开发的时候经常用到DataList.Repeater等,用这些控件的时候经常用到数据邦定,很多程序员都是按照MS提供的方 法<%#DataBinder.Eval(Contai ...

  2. 一个简单的Lua解释器

    #include "stdafx.h" #include<stdarg.h> #include<stdlib.h> #include<stdio.h& ...

  3. Python中optionParser模块的使用方法[转]

    本文以实例形式较为详尽的讲述了Python中optionParser模块的使用方法,对于深入学习Python有很好的借鉴价值.分享给大家供大家参考之用.具体分析如下: 一般来说,Python中有两个内 ...

  4. django 过滤器 、日期格式化参数

    http://blog.csdn.net/xyp84/article/details/7945094 django1.4 html页面从数据库中读出DateTimeField字段时,显示的时间格式和数 ...

  5. Windows 2003上 SaltStack/Salt 和 psutil 可能存在的问题及解决

    今天把salt安装在windows 2003上,发现无法启动,随之而来的是一个有一个的坑,让我们一起逐个排查. 问题一(salt无法启动) salt无法启动,错误结果如图:

  6. knockout 学习实例2 text

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  7. linux系统的初化始配置 IP 主机名 防火墙 selinux

    本次内容包括Linux:ip.主机名.关闭firewalld与selinux 开机临时生效和永久生效 ------------------------------------------------- ...

  8. 9、java中的final关键字

    /* final : 最终.作为一个修饰符, 1,可以修饰类,函数,变量. 2,被final修饰的类不可以被继承.为了避免被继承,被子类复写功能. 3,被final修饰的方法不可以被复写. 4,被fi ...

  9. 操作配置文件Properties

    // */ // ]]>   操作配置文件Properties Table of Contents 1 定义 2 读取配置值 3 修改和保存配置 4 注意 1 定义 csharp中在Settin ...

  10. ADF_Desktop Integration系列3_ADF桌面集成入门之重定义ADF Desktop Excel

    2013-05-01 Created By BaoXinjian