Android jumps to activity and delivers content through a child thread

Source: Internet
Author: User

The Android sub-thread cannot update the UI has been deeply rooted in my mind, it was taken for granted that the child thread is also not allowed to interface jump, but in the later study, found that it is possible to go through the child thread to the activity of the jump, Immediately shocked me. Get the hell out of the code and test it out. The results really can be. I want to make a note.



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; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); context = this;//the first button, jumps the activity through the main thread, and passes the content Findviewbyid (R.ID.BTN_GO1). Setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {log.e ("test", "Main thread:" + Thread.CurrentThread (). GetName () + "ID:" + thread.currentthread (). GetId ()); Intent Intent = new Intent (context, Otheractivity.class);//test pass string Intent.putextra ("Hello", "Hello im main thread"); StartActivity (intent);}); /second button, jump activity through a child thread, and pass content FIndviewbyid (R.id.btn_go2). Setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {New Thread ( New Runnable () {@Overridepublic void run () {LOG.E ("test", "Child Thread:" + thread.currentthread (). GetName () + "ID:" + thre Ad.currentthread (). GetId ()) Intent Intent = new Intent (context, otheractivity.class);//test pass string Intent.putextra (" Hello "," Hello im Child thread "); StartActivity (intent);}}). Start ();}});}}

The test concludes. Child threads can jump to activity. and passing content through intent


Android jumps to activity and delivers content through a child thread

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.