Events in Android: when implementing interfaces, it is better to use an external class or anonymous classes

This is an example of using an external class.

public class MyActivity extends Activity implements OnClickListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mylayout);
        findViewById(R.id.myid).setOnClickListener(this));
    }
    public void onClick(View v){...}
}

This is an example of anonymous classes.

public class MyActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mylayout);
        findViewById(R.id.myid).setOnClickListener(new OnClickListener() {
            public void onClick(View v){...}
        }));
    }
}
+3
source share
1 answer

Performance and efficiency are a little more important for Android. Something is considered optimized half-sophisticated, sometimes it makes sense in android. (For example, we should not use enum, but java int enum pattern). So, there is an answer to your question.

If you need to register multiple onClick listeners, use the implementation interface and use the switch key in it.

, . ( Android , , . ;))

+5

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


All Articles