Reputation: 9281
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:
Out of the four options I'm leaning towards the XML implementation as it seems cleaner, can anyone else give their opinion?
Upvotes: 2
Views: 370
Reputation: 8708
I don't know regarding memory efficiency, but Here's my approach.
onClick
if you have multiple buttons onCreate()
to be too big and messyonClick
isn'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.
Upvotes: 2
Reputation: 22527
Well it depends. At first I used to like the number 1(Make your Activity implement an OnClickListener interface) because the source look neat that way.
But at the end I settled with 2.Inner Class OnClickListener, because I found it more easier to read and more easier to implement, especially if you use eclipse and know the shortcuts of auto completion.
At the end I think it depends on taste.
Upvotes: 1