Android updates the UI in four ways

Source: Internet
Author: User

Android updates the UI in four ways
Preface

I believe that users who are new to Android development may encounter a problem. We started a thread and updated the UI in this thread, maybe a line of text is displayed in TextView, or the image displayed in ImageView is changed. Although it only seems simple and correct, the Android system makes your program crash with honor, and you still don't know why it is wrong. This is the most painful thing. I have been suffering this kind of pain, so that I don't want this kind of pain to spread any more, I decided to give you some ideas on how to update the UI, so that you can update the UI as you like in the Thread run method.

Implementation

Post method using Handler class

First, we need to generate a Handler Class Object in MainActivity. We don't need to implement the handMessage method, because the Handler Class Object serves as the message sending role instead of processing the message.

handler.post(new Runnable() {            @Override            public void run() {                mTextView.setText("OK");            }        });

In this way, we enter a Runnable to update the UI code, and then we can update the code without crashing.

Use Handler's own Message Processing Method
We know that Handler can send or process messages. In the first method, we use the message sending function. Now we use it to process messages. We need to generate a Handler object, and we need to overwrite its message processing method, and then implement what we need, as shown below:
private Handler handler = new Handler(){        public void handleMessage(android.os.Message msg){            mTextView.setText("ok");        };    };

As you can see, I overwrite the handleMessage method, and then add the UI update code in it. Of course, it is correct and the UI can be updated successfully. Then we need to call Handler's method of sending messages in MainActivity to send messages to ourselves before processing. We can simply call a method to send an empty message: handler. sendEmptyMessage (0 );

Update the UI in the runOnUiThread Method
runOnUiThread(new Runnable() {            @Override            public void run() {                mTextView.setText("updateUI->ok");            }        });

The runOnUiThread method can be used to update the UI according to its name. Because the UI must be updated in the UI thread, other threads are not allowed to update the UI, therefore, we pass in a Runnable in the runOnUiThread method, and then we can implement our operations in it. This method is very similar to the first method. I personally prefer this method because it does not need a Handler object. I think it is very convenient.

View calls the post method to update the UI
The other three methods are used to update the View through other classes and methods. The last method is that the View calls its own method to update the View, but they are still similar, the final implementation principle is similar.
mTextView.post(new Runnable() {            @Override            public void run() {                mTextView.setText("ViewUI->ok");            }        });

We also need to pass in Runnable and then update it.

Summary

Because the Android system does not allow us to update the UI in non-UI threads during design, we cannot connect to the network in the UI thread, if you do not know a friend, it may be thrown to death (I have already been pitted) for the security of the UI thread. If we need to update the UI thread, for example, to display the download progress and update the progress bar, we need an object that can communicate with non-UI threads in the UI thread and notify the UI thread to update non-UI thread requirements, I think Handler should be it. As for the importance of Handler, I will not talk about it. If you are interested, you can find some relevant information by yourself, understanding the meaning and usage of the Handler mechanism is helpful for development.

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: info-contact@alibabacloud.com 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.