Event distribution process: Activity –> ViewGroup –> View
When a click event is triggered, it’s passed to the activity first and then to the ViewGroup and finally to the View
1. The activity handles the click event
Start by listing the relevant methods
/** * Called to process touch screen events. You can override this to * intercept all touch screen events before they are dispatched to the * window. Be sure to call this implementation for touch screen events * that should be handled normally. * * @param ev The touch screen event. * * @return boolean Return true if this event was consumed. */ public boolean dispatchTouchEvent(MotionEvent ev) { if (ev.getAction() == MotionEvent.ACTION_DOWN) { onUserInteraction(); } if (getWindow().superdispatchTouchEvent (ev)) {return true; } // getWindow(). SuperDispatchTouchEvent (ev) returns false; A:} /** * getWindow().superdispatchTouchEvent (ev) * getWindow() = Get the object of the Window class * Window is implemented by the PhoneWindow class * PhoneWindow class implements abstract method */ @override public Boolean superDispatchTouchEvent(MotionEvent event) mDecor.superDispatchTouchEvent(event); // mDecor = top-level View (DecorView) / * * * mDecor superDispatchTouchEvent (event) * View (DecorView) * DecorView class is an inner class * DecorView PhoneWindow class inherits from FrameLayout, Is the parent of all interfaces *FrameLayout is a subclass of ViewGroup, DecorView indirect parent = ViewGroup */ public Boolean superDispatchTouchEvent(MotionEvent event) {return super.dispatchTouchEvent(event); // Call ViewGroup dispatchTouchEvent() // pass the event to ViewGroup for processing} Public Boolean onTouchEvent(MotionEvent event) {// Jump to the next four if (mWindow.shouldCloseOnTouch(this, event)) { finish(); Return true; } // return false; } 4: public boolean shouldCloseOnTouch(Context context, MotionEvent event) { final boolean isOutside = event.getAction() == MotionEvent.ACTION_UP && isOutOfBounds(context, event) || event.getAction() == MotionEvent.ACTION_OUTSIDE; if (mCloseOnTouchOutside && peekDecorView() ! = null && isOutside) {return true if the event is triggered outside the phone screen; } //false indicates that the event is triggered in the phone screen. }Copy the code
To sum up, when a time is generated, the flow of the activity is:
Start (touch screen) –> Activity dispatchTouchEvent(MotionEvent EV) –> Window superDispatchTouchEvent(MotionEvent Event) –> DecorView superDispatchTouchEvent(MotionEvent Event) –>ViewGroup dispatchTouchEvent()
DispatchTouchEvent () returns ture –> Event complete
DispatchTouchEvent () returns false –> Activity onTouchEvent(MotionEvent event) –> Window shouldCloseOnTouch(Context) Context, MotionEvent Event) –> End of event