With so many ways of implementing an OnClickListener within Android, I’m wondering whether there’s a best practice or a more recommended way of doing it over the others (ie: I remember reading certain ways require more memory than others)?
At the moment I know of four ways to implement the OnClickListener, these are:
- Make your Activity implement an OnClickListener interface.
- Inner Class OnClickListener.
- Inline Class OnClickListener.
- Use android:onClick attribute in XML definition of a Button.
Out of the four options I’m leaning towards the XML implementation as it seems cleaner, can anyone else give their opinion?
I don’t know regarding memory efficiency, but Here’s my approach.
onClickif you have multiple buttonsonCreate()to be too big and messyonClickisn’t too long, to keep the code readableBut in the bottom line, like @Lazy_Ninja said, it all comes down to taste. All 4 of them works.
I think what matters, when choosing, is keeping the code clean and readable.