在 android 中关闭对话框时是否需要删除 TextWatcher?

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

我想在我的对话框中监听 TextView 文本的变化,所以 我正在使用如下的 TextWatcher:

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

是否需要在关闭对话框时删除 TextWatcher,即在 dialog.setOnDismissListener() 中?或者如果我不删除它会怎样?

无需特别删除任何 listeners,一旦 dialog 关闭,其中的所有子 view 实例都将被销毁。因此,即使您不删除 TextListener 也不会发生任何事情,因为它附加到 EditText 也会被销毁 onDismiss.