Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
274 views
in Technique[技术] by (71.8m points)

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?

question from:https://stackoverflow.com/questions/65949801/is-there-any-need-to-remove-textwatcher-when-dialog-is-dismissed-in-android

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...