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. 导航代码position:relative

    <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="C ...

  2. SQL数据库的十条命令

    --(1)查询每个总学时数 select GradeId,SUM(classHour) from subject group by GradeId order by(SUM(classHour)) - ...

  3. Linux安装gcc编译器详解

    本人使用的是CentOS 6.5 64位系统,由于在安装系统的时候并没有勾选安装gcc编译器,因此需要自行安装gcc编译器. 使用yum安装gcc 对于配备了yum的Linux发行版而言,安装gcc编 ...

  4. knockout 学习实例3 html

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

  5. Provisional, Temporary 和Interim 的区别

    1 Provisional  adj. 临时的.暂时的.暂定的:n. 临时邮票 强调在一定时期内暂时的.双方同意的但还不是最终确定的决定或者条约等. Such as例如: Provisional go ...

  6. Hbase客户端API基础小结笔记(未完)

    客户端API:基础 HBase的主要客户端接口是由org.apache.hadoop.hbase.client包中的HTable类提供的,通过这个类,用户可以完成向HBase存储和检索数据,以及删除无 ...

  7. IOS启动页设置适应ios8/9

    直接上截图 1. 添加对应尺寸的图 2.设置三项 3.去掉默认设置 4.特别的xcode 7 需 通过在p.list配置文件中增加节点: Launch image 类型为String 设置值为Defa ...

  8. 导入Eclipse工程 到 Android Studio

    一.从Eclipse导入工程到Android Studio 根据官方的介绍,Android Studio可以兼容Eclipse的现有工程,但需要做一些操作: 首先升级ADT到最新版本,目前为版本号为2 ...

  9. 这些年我们一起搞过的持续集成~Jenkins+Perl and Shell script

    这些年我们一起搞过的持续集成~Jenkins+Perl and Shell script ##转载注明出处:http://www.cnblogs.com/wade-xu/p/4378224.html ...

  10. restfull api

    REST 表示状态传输.这是一个体系结构样式,可用于设计网络服务,可以被各种客户端消耗.核心思想是,不使用如CORBA,RPC或SOAP复杂的机制在机器之间进行连接,简单的 HTTP 用于使它们之间调 ...