Android Subnet threads can't update UI?

Source: Internet
Author: User

In the actual development, the child thread must not be able to update the UI, but in the test code, we can implement the function of updating the UI in the child thread, for example: a textview in the OnCreate life cycle method Findviewbyid

And then write a thread, directly to this textview in the online settext, you will find that the miracle unexpectedly happened, actually updated the UI, and the app did not cresh off, suddenly shocked

Then each attempt in the OnStart Onresume method in the sub-thread update UI operation, unexpectedly can be updated, but there is a place to note that when the Onresume life cycle method of our program is finished, then update the UI in the child thread will be an error, for example, Update the UI in a button's click event, at which point the onresume must have been executed, and it will be an error at this time. The reason is that at the first time the OnCreate onstart onresume Life methods. The Rootviewimpl of activity has not yet been created. There is no problem with updating the UI at this time. But when Rootviewimpl is created, You can no longer update the UI in a child thread. So the way in which the UI is updated in a child thread is completely useless in the actual development. A thread hanler to update the UI in a critical moment


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 thre Ad,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");}}

Android Subnet threads can't update UI?

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.