What is the difference between onKey(), OnKeyDown() and dispatchKeyEvent() methods provided by Android?
I Would like to know when and where each of these can be used.
Please shed some light into this.
What is the difference between onKey(), OnKeyDown() and dispatchKeyEvent() methods provided by Android? I
Share
Tracing the source code of the 5.1 Source for the View Class. It would seem that
dispatchKeyEvent()is the first method called by the system. Overloading it will prevent any and all key events from being called unless the base version is called.dispatchKeyEvent()‘s first move is to attempt to pass the event to anonKeyListenerif there is one. This is whenonKey()is called. If theonKey()implementation returnstrue,dispatchKeyEvent()will return there and other events will not be called.If there is no
onKeyListeneror theonKeyListener‘sonKey()method returnedfalse,dispatchKeyEvent()will then call theKeyEvent‘sdispatch()method. Which will then in turn call all the methods in theKeyEvent.Callbackinterface on your view. This includesonKeyDown()andonKeyUp().