Event distribution is a headache in Android development, and one of the most frequently asked questions during interviews. Also, understanding the process of event distribution can help us deal with the sliding between components that we encounter during development. So this is also the key knowledge point to see when reviewing. When preparing the materials for this article, I also drew the flow chart on the draft paper several times, but I always felt it was not as good as the flow chart of the author of “Illustrated Android Event Distribution Mechanism”, so I copied the flow chart of this author in the article (shame).

directory

  • The event type
  • Event distribution process and sequence
  • Three stages of distribution
  • Log analysis

1. Event type

The MotionEvent class is used to determine the type of events that occur when our hand touches the screen. Common event types fall into three categories:

  • ACTION_DOWNN: The finger just touches the screen, marking the start of the touch event

  • ACTION_MOVE: Move your finger on the screen

  • ACTION_UP: The finger is lifted from the screen to signal the end of the event

Under normal circumstances, our interaction with the phone will trigger a series of events, such as clicking event will trigger ACTION_DOWNN- >ACTION_MOVE, and sliding event will trigger ACTION_DOWNN- >ACTION_MOVE- >ACTION_UP.

2. Event distribution process and sequence

Event distribution process: Event transmission can be analyzed from the click event. After the click event is triggered, the system distributes the event layer by layer and finally transfers it to a specific view for processing. The event transmission process is the event distribution process.

The order in which events are delivered is Activity–>ViewGroup–>View

3. Three phases of distribution

The above flow chart can be used in conjunction with the study of event distribution

dispatchTouchEvent

Distribution event phase

This event can be handled in several ways:

Processing in an Activity

  • The event will be consumed regardless of whether the method returns true or false in the Activity, and the event will not be propagated.
  • Returns a method of the same name as its parent: that issuper.dispatchTouchEvent(ev)Then the event will continue to spread

The processing of ViewGroup

  • Returns true: the event is consumed and will not be propagated.
  • Return false and the event will be returned, calling the Activity’s onTouchEvent method.
  • Parent method with the same name: distributes events to its own onInterceptTouchEvent method

Processing in View

  • Returns false or a method of the same name as the parent, and the event will be returned. Events are handled back to the onTouchEvent passed to their parent container.
  • Returns true: the event is consumed and no longer propagated.

onInterceptTouchEvent

Intercept the event, which only ViewGroup has.

  • Returns false or a method of the same name as its parent, which means that the event is not intercepted and will be passed to its child View
  • Return true: Intercepts the event. Call your own onTouchEvent to handle this event.

onTouchEvent

Consumptive event stage

This event can also be handled in the following ways

Processing in an Activity

  • Events are handled in this method regardless of whether it returns false, true, or a method of the same name as its parent. The event propagated here means that neither the ViewGroup nor the View of the window handled the event.

Processing in ViewGroup

  • Returns false or a method with the same name as its parent: means it does not process either, and the event will be passed back to the Activity’s onTouchEvent for processing. Subsequent events will not be passed to him.

  • Returns true: the event is consumed and will not be returned. Subsequent events are left to him, not passed to its child views

Processing in View

  • Returns false or a method with the same name as its parent: means it does not process and the event will be passed back to its parent’s onTouchEvent. Subsequent events will not be passed to him.

  • Returns true: the event is consumed and will not be returned, and subsequent events will continue to be passed to it for processing.

4. Log analysis:

All of the following log information can be viewed in the Android event distribution log file in the GitHub test project doc folder.

Normally log messages (Activity, ViewGroup, View all return methods of the same name as their parent)

View, ViewGroup, and Activity onTouchEvent returns the same log message for the parent class or false with the same name as below (the Activity returns true), so it is not pasted out in the article.

From the above log we can see that when we click a View on the screen, Events are sent from EventActivity’s dispatchTouchEvent to TestFrameLayout’s dispatchTouchEvent and then to TestView’s dispatchTouchEvent (onInterce) PtTouchEvent is not intercepted) is ultimately passed to TestView’s onTouchEvent method. As mentioned earlier, if the View’s onTouch returns false or a method of the same name as its parent class, it will not handle the event, and the event will be returned. As you can see from the above log, the DOWN event is passed back to the onTouchEvent of TestFrameLayout. TestFrameLayout is also a method of the same name that returns the parent class, and the event will continue to be sent back eventually to the onTouchEvent of the EventActivity. As you can see from the above information, the subsequent UP event is not sent down, but continues to be handled by the Activity’s onTouchEvent.

The View’s onTouchEvent returns true

When the View’s onTouch returns true, the event is handled. This event will not be returned, and the subsequent events will be in his hands. You can analyze the information in the red box below

Log message when ViewGroup onTouchEvent returns true

The event is sent to TestView, a child component of the ViewGroup, but it does not process it. The event is passed back to the ViewGroup(the log in the yellow box). Its onTouchEvent returns true, indicating that the event was consumed by the ViewGroup and subsequent events will be sent to it for processing.

When onInterceptTouchEvent returns true log information

If this method returns true, the event will be handled by TestFrameLayout’s own onTouchEvent. TestView’s onTouchEvent method was not called.

DispatchTouchEvent returns a true log

ViewGroup dispatchTouchEvent (); ViewGroup dispatchTouchEvent (); ViewGroup dispatchTouchEvent (); The ViewGroup passes the event to the Activity’s dispatchTouchEvent method. So for the sake of space, I’m pasting in the View log.

If dispatchTouchEvent returns true, the event is consumed and the event will not be sent down. The follow-up will be left to him. (Same with View)

DispatchTouchEvent returns false

If dispatchTouchEvent returns false, the event will not be sent down and will be passed back to its parent’s onTouchEvent method. The follow-up will be handled by his superiors. (Same with View)

Put an end to

In order to really understand the event distribution, or to write a demo test to easily understand, this article is still a little messy. If you don’t know what to say after reading this article, take a look at the following two links, which are two of the articles I have read about event distribution. It is recommended to first read the illustrated Android Event Distribution Mechanism, and be sure to understand the flow chart drawn by the author.

Reference:

Illustrate the Android event distribution mechanism

Android event Distribution mechanism explanation: the most comprehensive, most understandable