Android interview questions-event processing, android questions-Events

Source: Internet
Author: User

Android interview questions-event processing, android questions-Events
1. Handler Mechanism

In Android, the main thread is also called the UI thread. We also know from the name that the main thread is mainly used to create and update the UI, while other time-consuming operations, such as network access or file processing, multimedia Processing and other operations must be performed in sub-threads. The reason why operations are performed in sub-threads to ensure the smoothness of the UI is that the refresh frequency displayed on the mobile phone is 60Hz, that is, 60 refresh times per second, refresh Every 16.67 milliseconds. To avoid Frame loss, it is recommended that the main thread process code should not exceed 16 milliseconds. After the sub-thread processes the data, Android only allows the main thread to modify the UI to prevent confusion in the UI processing logic. At this time, Handler is required to act as a bridge between the sub-thread and the main thread.
We usually declare Handler in the Activity, and then override the handleMessage method in Handler. When the sub-thread calls the handler. sendMessage () method, the handleMessage method will be executed in the main thread. In addition to Handler and Message, there are hidden logoff and MessageQueue objects. By default, the logoff. preper () method has been called by Android in the main thread. The purpose of this method is to create a MessageQueue variable in logoff and bind the logoff object to the current thread. When the sendMessage (object) method of Handler is called, the Message object is added to the MessageQueue queue created by logoff, and the target object is specified for the Message. In fact, the target object is the Handler object. Main thread
The logoff. logoff () method is executed. This method extracts the Message from the logoff member variable MessageQueue, and then calls the handleMessage () method of the target object of the Message. This completes the entire message mechanism.

2. Event distribution mechanism

2.1 What is the difference between onTouch and onTouchEvent in event distribution?
Both methods are called in View dispatchTouchEvent. onTouch takes precedence over onTouchEvent. If the onTouch method returns true to consume the event, the onTouchEvent will not be executed. In addition, onTouch requires two prerequisites for execution. The value of the first mOnTouchListener cannot be blank, and the control currently clicked must be enable. Therefore, if you have a control that is not enable, registering an onTouch event for it will never be executed. For this type of control, if we want to listen to its touch event, we must rewrite the onTouchEvent method in the control.

2.2 describe Android's event distribution mechanism

Android's event distribution mechanism is mainly Touch event distribution, which has two main roles: ViewGroup and View. The Touch event of an Activity is actually a Touch event that calls its internal ViewGroup and can be processed as a ViewGroup directly. In ViewGroup, ViewGroup can also be in other viewgroups. In this case, the internal ViewGroup is used as View for analysis.
First, analyze the processing process of ViewGroup. First, we need to have a structure model concept: ViewGroup and View form a tree structure, with the top layer being the ViewGroup of Activity. Below there are several ViewGroup nodes, there are several ViewGroup nodes or View nodes under each node, and so on.


When a Touch event (for example, a Touch event) arrives at the root node, that is, the ViewGroup of Acitivty, it is delivered in sequence, and the process of delivery is implemented by calling the dispatchTouchEvent method of the child View (ViewGroup. To put it simply, ViewGroup traverses the child View it contains and calls the dispatchTouchEvent method of each View. When the child View is a ViewGroup, the dispatchTouchEvent method of the ViwGroup will be called to continue calling the dispatchTouchEvent method of its internal View. In the above example, the message delivery order is as follows: ①-②-⑤-⑥-7-③-④.
The dispatchTouchEvent method is only responsible for event distribution. It has a boolean type return value. If the return value is true, sequential delivery is interrupted. In the preceding example, if the return result of the 5th dispatchTouchEvent is true, neither of them will receive this Touch event.

1. There are only two main angles in Touch event distribution: ViewGroup and View. The ViewGroup package contains onInterceptTouchEvent, dispatchTouchEvent, and onTouchEvent. View contains dispatchTouchEvent and onTouchEvent. ViewGroup inherits from View.

2. ViewGroup and View form a tree structure. The root node is a ViwGroup contained in the Activity.

3. A touch event consists of Action_Down, Action_Move, and Aciton_UP. Among a complete touch event, there is only one Down event and one Up event. There are several Move events, which can be 0.

4. When Acitivty receives a Touch event, it traverses the sub-View to distribute the Down event. The traversal of ViewGroup can be seen as recursion. The purpose of distribution is to find the View that really needs to process the complete touch event. This View will return true in the onTouchuEvent result.

5. When a sub-View returns true, the distribution of the Down event is aborted and the sub-View is recorded in the ViewGroup. The next Move and Up events are processed by the sub-View. Because the child View is saved in ViewGroup, when the node Structure of the multi-layer ViewGroup, the parent ViewGroup saves the ViewGroup object where the View that actually handles the event is located, such as the structure of the ViewGroup0-ViewGroup1-TextView, textView returns true, which is saved in ViewGroup1, and ViewGroup1 returns true, which is saved in ViewGroup0. When a Move or UP event occurs, it is first transmitted from ViewGroup0 to ViewGroup1, and then transmitted from ViewGroup1 to TextView.

6. When no Down event is captured for all sub-views in the ViewGroup, The onTouch event of the ViewGroup is triggered. The trigger method is to call the super. dispatchTouchEvent function, that is, the dispatchTouchEvent method of the parent Class View. The onTouchEvent method of Acitivity is triggered when no child View is processed.
7. onInterceptTouchEvent has two functions: 1. Intercept the distribution of Down events. 2. Stop the Up and Move events and send them to the target View so that the ViewGroup where the target View is located can capture the Up and Move events.

3. The subthread sends a message to the main thread to update the UI, except handler and AsyncTask,What else?

(1) Update the UI using the runOnUiThread method of the Activity object in the Child thread using the runOnUiThread () method:

(2) Update the UI using the View. post (Runnable r) Method

4,Can new handler be used in a child thread? Why?

No. If new Handler () is used directly in the Child thread, an exception occurs in java. lang. RuntimeException: Can't create handler inside thread that has not called.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.