Event Handlers and Event Listeners

Most user interaction with an Android device is captured by the system and sent to a corresponding callback method. For example, if the physical Back button is pressed, the onBackPressed() method is called.These events can be handled by extending the class and overriding the methods, called event handlers.

User interaction with View or ViewGroup objects can also support event listeners.These are methods that wait for the registered event and then trigger the system to send the event information to the corresponding callback method. For example, the setOnClickListener() event listener can be registered for a button and when it is pressed, the onClick() method is called.

Event listeners are the preferred method when available because they avoid the class extension overhead. Furthermore, an Activity implementing an event listener gets a callback for all the layout objects it contains, allowing for more concise code. Both event listeners and event handlers are demonstrated in this chapter within the context of handling physical key press events and screen touch events.

Character Building Thought Power

Character Building Thought Power

Character-Building Thought Power by Ralph Waldo Trine. Ralph draws a distinct line between bad and good habits. In this book, every effort is made by the writer to explain what comprises good habits and why every one needs it early in life. It draws the conclusion that habits nurtured in early life concretize into impulses in future for the good or bad of the subject.

Get My Free Ebook


Post a comment