Having a solid experience in non-Java and non-Android area, I'm learning Android.
I have a lot of confusion with different areas, one of them is how to handle button clicks. There are at least 4 way of doing that (!!!), they are briefly listed here
for consistency purpose I will list them:
Have a member of the
View.OnClickListener
class in the activity and assign it to an instance that will handleonClick
logic in theonCreate
activity method.Create 'onClickListener' in the 'onCreate' activity method and assign it to the button using setOnClickListener
Implement 'onClickListener' in activity itself and assign 'this' as a listener for the button. For the case if activity has few buttons, button id should be analyzed to execute 'onClick' handler for the proper button
Have public method on the activity that implements 'onClick' logic and assign it to the button in the activity xml declaration
Question #1:
Are those all methods, is there any other option? (I don't need any other, just curious)
For me, the most intuitive way would be the latest one: it requires the least amount of code to be typed and is the most readable (at least for me).
Though, I don't see this approach used widely. What are cons for using it?
Question #2:
What are pros/cons for each of these methods? Please share either your experience or a good link.
Any feedback is welcome!
P.S. I've tried to Google and find something for this topic, but the only things I've found are description "how" to do that, not why is it good or bad.