This article has been collected to sort out the interview questions (click Tab at the bottom of the home page).
In the last article on the official account, I asked a question, as shown below:
To get the answer, you need to know the difference between runOnUiThread, handler. post, and view.post.
RunOnUiThread: runOnUiThread: runOnUiThread: runOnUiThread: runOnUiThread
If it is not currently a UI thread, the main thread Handler throws a message to MessageQueue. If the current UI thread is executed immediately.
[234] should be [324], because the first runOnUiThread will be executed immediately, and [24] will be executed according to the order in which MessageQueue is entered.
[234] is the first view. post? This is a little bit of a mess.
The trap they set, as you can see, is that the View instantiated is not added to any ViewGroup that can attachToWindow, which is a big trap.
View.post has been implemented differently since api24, which means that the answer to the original question will be different on Android 7.0 (api24) and below.
Take a look at the source code on API24:
Look at the source code on API23:
See the difference: when mAttachInfo is null, the source code changes.
Let’s look at the same part of the code, when mAttachInfo is not null, and see when it is assigned:
View#dispatchAttachedToWindow
“DispatchAttachedToWindow” (” dispatchAttachedToWindow “);
ViewGroup#dispatchAttachedToWindow
So who is this ViewGroup? And who called it?
Let’s see who called it first:
ViewRootImpl#performTraversals
The caller knows, so who is host? Host is the Activity’s DecorView, and we’re not going to talk about it in this article. We just know that in order for a child View to call dispatchAttachedToWindow, it must be a child of a DecorView. The View we created in [1] is not a child of the DecorView, so the dispatchAttachedToWindow method of the View in [1] will not be executed. So the original mAttachInfo is null in this problem.
The reason for a long time, just api23 and API23 above the same part of the source code explained, so not the same source code? That’s when mAttachInfo is null, so take your time.
As mentioned above, our [1] example would make mAttachInfo null and execute viewrootimpl.geTrunQueue ().post(action) on api23 and below; The API24 starts by executing getRunQueue().post(action); .
To see the difference between the two, API23 is to use the ViewRootImpl RunQueue:
Queues are static ThreadLocal objects. The queues used by the main queues are the same RunQueue. When will the Runnable in the RunQueue be executed?
ViewRootImpl#performTraversals
The performTraversals method is the start of the View rendering process. At this point, api23 and the following view. post code will be executed, so the answer in the question will be [3241] in api23 and below. Use getRunQueue in View directly:
It is not using the main thread RunQueue, but its own object, so when will its own RunQueue be executed? “DispatchAttachedToWindow” does not execute “dispatchAttachedToWindow” for View [1].
After all analysis, the correct answer should be [324] / [3241], need to distinguish API version. Thank you for your participation. I hope this analysis can be helpful to you.