banner
jzman

jzman

Coding、思考、自觉。
github

Android Event Distribution Process Analysis

PS: Don't be too anxious, it is most important to solidly enrich yourself.

In the previous article, a basic overview of the event methods in Activity, ViewGroup, and View was provided. Now, let's summarize the event propagation in Android through examples.

  1. Default event dispatching process
  2. Event dispatching
  3. Event handling
  4. Event interception
  5. Summary

Default event dispatching process#

The normal flow of Android event dispatching, which is how events are dispatched by default. First, create custom ViewGroup and custom View by inheriting LinearLayout and TextView respectively. Then, override the relevant event methods for event dispatching in each of them. The Activity also overrides its own event methods. By observing the logs, we can observe the normal event dispatching process. The layout is shown in the following figure:

image

By triggering the onTouch event of MTextView and default handling the event dispatch methods, the execution flow is shown in the following figure:

image

Clearly, the starting point of event dispatching is the dispatchTouchEvent() method of the Activity. Then, the events are dispatched to the ViewGroup through a series of action events. Specifically, this is done by calling the dispatchTouchEvent() method of the ViewGroup. The onInterceptTouchEvent() method of the ViewGroup returns false by default, which means that events are not intercepted and are passed to the child View (the ViewGroup is also a child View, such as MRelativeLayout). Then, the events are continuously dispatched downwards until they reach the innermost child View.

Of course, when the event is dispatched to the child View, the dispatchTouchEvent() method of the child View is called. The event dispatch method internally calls the onTouchEvent() method. onTouchEvent() returns false by default, indicating that the View does not consume the event. If it returns true, it means that the event is consumed. The following figure shows the default event dispatching process based on the above execution results:

image

Through the above verification, we can at least understand the execution order of the various event-related methods.

Event dispatching#

The dispatchTouchEvent() method is responsible for event dispatching, determining whether the current event is consumed by itself or continues to be dispatched to the child View. Returning true means that the event has been consumed and subsequent events will continue to be executed, such as ACTION_MOVE and ACTION_UP events. Returning false means that the event will continue to be dispatched to the child View. If the child View is a ViewGroup, the event will be first dispatched to the onInterceptTouchEvent() method of the ViewGroup to determine whether to intercept the event. In the above example, the dispatchTouchEvent() method of MRelativeLayout returns true, indicating that the event is consumed and subsequent events are received. The log screenshot is shown in the following figure:

image

Clearly, when the dispatchTouchEvent() method of MRelativeLayout returns true, the event is not propagated downwards. In order to observe the execution of each event method more clearly, specific events (such as ACTION_DOWN) are not shown in the log. Therefore, when the dispatchTouchEvent() method returns true, it means that the event has been consumed.

Event interception#

The onInterceptTouchEvent() method is responsible for event interception. Its return value determines whether to intercept the current event. Returning true means intercepting the current event, and returning false means not intercepting the event. By default, the implementation continues to call the dispatchTouchEvent() method of the child View for event dispatching. In the above example, the onInterceptTouchEvent() method of MRelativeLayout is set to return true, intercepting the current event and preventing it from being propagated downwards. The log screenshot is shown in the following figure:

image

After event interception, event handling needs to be performed. Of course, event handling is the responsibility of the onTouchEvent() method. onTouchEvent() can handle the event or not. If onTouchEvent() returns true, it means that the event is handled. If it returns false, the event is handed over to the onTouchEvent() method of the parent View for handling. In the case shown in the figure, MRelativeLayout intercepts the event but does not handle it, so it is ultimately handled by the Activity.

Event handling#

The onTouchEvent() method is responsible for event handling. Its return value determines whether the View consumes the event. Returning true means that the current View consumes the event, and returning false means that the event is not consumed and is passed to the parent View. In the above example, the onTouchEvent() method of MRelativeLayout is set to return true. The log screenshot is shown in the following figure:

image

The above screenshot omits several ACTION_MOVE events. When an event is successfully intercepted by a View, a series of events after ACTION_DOWN will be directly handled by that View.

Summary#

  1. Event dispatching is the process of dispatching events from the parent View to the child View. If the event is not intercepted or handled during the dispatching process, when it reaches the deepest level View, it will be passed back up and ultimately handled by the Activity. Subsequent events will not be received. If a View intercepts an event and handles it, the event will be handled by the onTouchEvent() method of the intercepting View. If a View intercepts an event but does not handle it, the event will stop being dispatched to the child View and will be passed to the onTouchEvent() method of the parent View.
  2. After an event is successfully intercepted, it needs to be handled. The handling of events is mainly done by the onTouchEvent() method of the intercepting View or its parent View. Whether to handle the event depends on the return value of the corresponding onTouchEvent() method.
  3. The handling of events is done by the onTouchEvent() method. Returning true means that the event is handled, and returning false means that the event is not handled and will continue to be passed upwards until it is handled.
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.