androiddialogandroid-textwatcher

Is there any need to remove TextWatcher when dialog is dismissed in android?


I want to listen to TextView text changes in my dialog, So I am using TextWatcher like below:

 final View layout = activity.getLayoutInflater().inflate(R.layout.popup_history,
                    container, false);
            final Dialog dialog = new Dialog(context, R.style.full_screen_dialog);

            dialog.setContentView(layout);
            final Window window = dialog.getWindow();
            WindowManager.LayoutParams layoutParams;
            if (window != null) {

              

                window.setLayout(WindowManager.LayoutParams.MATCH_PARENT,
                        WindowManager.LayoutParams.MATCH_PARENT);
                layoutParams = window.getAttributes();
                if (layoutParams != null) {
                    layoutParams.windowAnimations = R.style.ChooserStyle;
                    layoutParams.gravity= Gravity.BOTTOM;
                    window.setAttributes(layoutParams);
                }
            }


           


            TextView searchHistoryTV = layout.findViewById(R.id.searchHistoryTV);


            searchHistoryTV.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    CharSequence editable = searchHistoryTV.getText();
                    //thereafter use this CharSequence

                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });


            dialog.setCanceledOnTouchOutside(true);
            dialog.setCancelable(true);
            dialog.show();

Is there any need to remove TextWatcher when dialog is dismissed i.e. in dialog.setOnDismissListener()?Or what if I don't remove it?


Solution

  • No need to remove any listeners specifically, once the dialog dismisses, all the child view instances inside it will be destroyed anyways. So nothing happens even if you do not remove TextListener as it is attached to EditText also gets destroyed onDismiss.