Is adding and removing event listeners more efficient than not having them at all?

Say I have a 'SWITCH BUTTON' and a regular 'BUTTON'. If the SWITCH BUTTON button is on, then NORMAL BUTTON is on. If the SWITCH key is off, the NORMAL BUTTON is disabled.

Under these conditions, is it more efficient to add and remove a NORMAL BUTTON event listener to free up memory consumption when it is turned off?

For example:

public void sample() {
    Switch switchButton = (Switch) findViewById(R.id.sample_switch);

    switchButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        Button normalButton = (Button) findViewById(R.id.sample_button);

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked) {
                normalButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //Some codes...
                    }
                });
            } else
                normalButton.setOnClickListener(null);
        }
    });
}
+4
source share
1 answer

, , . EventListener , , , , .

, , , , . , . , , .

, , , , , , , , .

+3

Source: https://habr.com/ru/post/1621619/


All Articles