Does the android sub-thread really fail to update the ui? The android thread updates the ui.

Source: Internet
Author: User

Does the android sub-thread really fail to update the ui? The android thread updates the ui.

In actual development, the sub-thread cannot update the ui, but in the test code, we can update the ui in the sub-thread, for example: A textview performs findviewbyid in the oncreate lifecycle method.

Then, write a thread and perform settext on the textview directly in the thread. Then you will find that the miracle actually happened, And the ui was actually updated, and the app didn't actually get cresh, suddenly stunned

Then try to update the ui of the sub-thread in the onstart onresume method separately. You can update all the sub-threads, but note that, when the onresume life cycle method of our program is executed, an error will be reported when the ui is updated in the Child thread. For example, update the ui in a button click event, at this time, onresume must have been executed, and an error will be reported at this time. the reason is in the first oncreate onstart onresume life method. activity rootviewimpl has not been created yet. there is no problem in updating the ui at this time. however, after rootviewimpl is created, the ui cannot be updated in the Child thread. therefore, the method of updating the ui in the Child thread is completely unavailable in actual development. at the crucial moment, we should use the thread hanler method to update the ui.


The following is an example program:



Activity:

package com.example.testchildthreadgootheractivity;import android.app.Activity;import android.content.Context;import android.content.Intent;import android.os.Bundle;import android.util.Log;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;public class MainActivity extends Activity {private Context context;private TextView tv_result;private Button btn_refresh;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);context = this;initVeiw();Log.e("test", "main thread,name:" + Thread.currentThread().getName()+ ",id:" + Thread.currentThread().getId());new Thread(new Runnable() {@Overridepublic void run() {Log.e("test", "child thread,name:" + Thread.currentThread().getName()+ ",id:" + Thread.currentThread().getId());tv_result.setText("oncreate refresh textview");}}).start();}@Overrideprotected void onStart() {super.onStart();new Thread(new Runnable() {@Overridepublic void run() {Log.e("test", "child thread,name:" + Thread.currentThread().getName()+ ",id:" + Thread.currentThread().getId());tv_result.setText("onstart refresh textview");}}).start();}@Overrideprotected void onResume() {super.onResume();new Thread(new Runnable() {@Overridepublic void run() {Log.e("test", "child thread,name:" + Thread.currentThread().getName()+ ",id:" + Thread.currentThread().getId());tv_result.setText("onresume refresh textview");}}).start();}private void initVeiw() {tv_result = (TextView) findViewById(R.id.tv_result);btn_refresh = (Button) findViewById(R.id.btn_refresh);btn_refresh.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {new Thread(new Runnable() {@Overridepublic void run() {Log.e("test", "child thread,name:" + Thread.currentThread().getName()+ ",id:" + Thread.currentThread().getId());tv_result.setText("onresume refresh textview");}}).start();}});}@Overrideprotected void onPause() {super.onPause();Log.e("test", "onPause");}@Overrideprotected void onStop() {super.onStop();Log.e("test", "onStop");}@Overrideprotected void onDestroy() {super.onDestroy();Log.e("test", "onDestroy");}@Overrideprotected void onRestart() {super.onRestart();Log.e("test", "onRestart");}}

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.