The best way to handle events in Android

advertisements

I know this seems like a duplicate question, but i was really unable to find a good answer on the related topic.

There are tons of questions about what is the best way to handle an OnClick event of a Button.
Here are some of the options i came across:

1 - Defining a listener programatically on the OnCreate method:

button.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View v) {
        //do stuff
    }
});


2 - Setting the android:OnClick property on the XML:

<Button android:id="@+id/btnDelete"
    ...
    android:OnClick="btnDelete_OnClick"/>


3 - Implementing the OnClickListener interface on the Activity class and passing a self reference to the Button:

public class MainActivity extends Activity implements OnClickListener{
    @Override
    public void onClick(View v) {
        //do stuff
    }

    protected void onCreate(Bundle savedInstanceState) {
        ...
        button.setOnClickListener(this);
    }
}


4 - Create a field with the OnClickListener type:

private OnClickListener onClickHandler = new OnClickListener(){

    @Override
    public void onClick(View v) {
        //stuff
    }
};

protected void onCreate(Bundle savedInstanceState) {
    ...
    button.setOnClickListener(onClickHandler);
}


When it comes to a Button and the OnClick event, I would always prefer defining it on the XML, it is just more clean.

But what about other events like the OnItemClick from the ListView or the OnTimeSet from the TimePickerDialog? There is no property I can see for setting it on the XML. I think that implementing the Listener interface is a pretty clean solution, but that would mean I can only implement it once, and if I have two equal Views I would have to handle their events in the same place. If I use option 2 or 4, it would probably get quite messy when handling several events from different Views from the UI.

I would like to see other opinions on this subject, if there are any other options of implementation for event handling. Is there really an alternative that could be defined as the better one or is it just a personal matter of each programmer?


Let me try to explain case by case:

Case# 1 This way will create anonymous clases as much as you create buttons (every button will need new listener ), and its less readable and costly.

Case# 2 Actually if you read the code behind this, you will find it use reflection to find your listener (method) of callback, and its less readable, and confuses other developers.

Case# 3 This way is hard to navigate through, because you can't determine the type of the listener you are using with current button (I know eclipse will highlight the methods this are pointing at, but with huge code I think it will be hard to find).

Case# 4 I think this is the best way to implement the listeners,easy to navigate to, more readable, one listener can handle all related events (and with using eclipse, just ctrl+click you can go to the listener), so I recommend this (am using only this way at work)

I hope this will help