Detailed explanation of four methods of Android updating UI

preface

I believe every Android Developer knows that updating the UI can only be carried out in the main thread. If it is necessary to update the UI after the sub thread executes the task, you need to jump to the main thread with the help of handler. The following describes several ways to operate the UI.

1、 Use handler's handlemessage()

Construction of handler

It can be seen that looper is required for constructing handler. If not, an exception will be thrown. Looper. myLooper()

A looper object will be returned. How to return it? Look at the source code

Obviously, myloop () fetches the looper object from ThreadLocal. So the question is, when do we save the looper object into ThreadLocal? The secret is that we call looper When preparing (), look at the source code

It can be seen that when the looper object retrieved by sthreadlocal is empty, a new looper object will be created.

Thus, before instantiating a hanlder, you need to call looper Prepare(), otherwise an exception will be thrown, unless the handler is built in the main thread. Because the system has established a looper for the main thread when it is created.

Creation of message

In general, the most common practice is

However, to do so, we need to create a new message every time, which is a waste of space. We can use obtainmessage(); Look at the source code

This method returns message Obtain (this, obj), that is, the same function as obtainmessage (), so you can also use message directly obtain(this,obj) 。

Look at the source code of obtain ()

Looking at the above code, we also have to find the nonparametric structure of obtain

It is not difficult to see that obtain returns a message object from the message pool. If the message pool is empty, a new message will be created, so it is generally reasonable to use obtain() to get a message.

Parameters of message

If you only need to store integer data, use arg1 and arg2

Any object sent to the receiver. When using the message object to deliver messages between threads, if it contains a Parcelable structure class (not implemented by the application), this field must be non null.

User defined message code, so that the receiver can understand the information of the message. Each handler contains its own message code, so you don't have to worry about conflicts between customized messages and other handlers.

If you need to pass a bundle object, use SetData (bundle)

The handler object that receives this message.

Several methods of processing messages

Through the above analysis, no matter which SendMessage () method the handler calls, the message is finally inserted into the queue without other operations. Via queue Using the method enqueuemessage (MSG, uptimemillis), we also know that the order in which looper processes messages is polling according to the time of message insertion.

Add the method of empty message

2、 Handler's post() method

Use steps

Take a look at the source code of the post method

Seeing this, I suddenly realized that the post method actually sent a message.

Look at the source code of getpostmessage (R)

It is clear that this method encapsulates the runnable object as a message, and then sends sendmessagedelayed to the handler for processing.

Here, we need to know how messages are taken out. If there are mmessages (i.e. pending messages) in the current messagequeue, take out this message, and then make the next message mmessages. Otherwise, it will enter a blocking state until a new message enters the queue. Whenever a message is fetched, it is passed to MSG In the dispatchmessage () of target, this msg Target is MSG in the enqueuemessage method of the handler Target = this statement, that is, the current handler object. So the next focus is naturally to go back to dispatchmessage()

If handler. Is called Post() method, then callback= Null, so handlecallback (MSG) is executed; method

That is, the runnable object is finally processed by the handle. According to the principle of handler, the handler needs to instantiate the post method in the main thread to update the UI.

3、 Post method of view

Look at the source code

At a glance, the essence of calling the post method of handler is the same.

4、 Runonuithread() method in activity

There must be nothing to say about this method. If it is a UI thread, run it directly. If not, use handler One thing to note about post () is that this method can only run in an activity.

summary

The above is the whole content of this article. I hope it can bring some help to your study or work. If you have any questions, you can leave a message.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>