Search code examples
androidandroid-fragmentstextwatcher

cannot change value of edittext due to textwacher in fragment


I have a listview in a fragment. By clicking on listview items ,opens a new fragment to show details.In second detail fragment i have a edittext and have a textwatcher on that edittext.On entering text in edittext textwatcher does its operations.On back button i removed detail fragment and back in listview fragment. But the problem is when i open detail fragment second time edittext remains with its previous value.textwatcher sets the old fragment value in new detail fragment.so how to deal with it. Please help me out.

public class Recieptlistfrag extends Fragment {.....

RecieptFragment fragment;
SharedPreferences preferences;
............
............


listView.setOnItemClickListener(new OnItemClickListener() {
fragment.setArguments(bundle);
getActivity().getSupportFragmentManager()
                        .beginTransaction().add(R.id.container_top, fragment,
                                "RecieptFragment").commit();
...........

now in back button of receitfragment

getActivity().getSupportFragmentManager()
                            .popBackStackImmediate();

edittext with textwatcher...

    public static class MyTextWatcher implements TextWatcher
{
     public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
            customAdapter.getFilter().filter(cs);
        }

        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
        }

        public void afterTextChanged(Editable arg0) {
        }
}

Solution

  • Don't do :

    RecieptFragment fragment;
    

    Do this :

     listView.setOnItemClickListener(new OnItemClickListener() {
    
            Fragment fragment = new RecieptFragment();
                fragment.setArguments(bundle);
                getActivity().getSupportFragmentManager().beginTransaction().add(R.id.container_top, fragment,"RecieptFragment").commit();
    

    Hope this will help. :)