Android-activity use (2)-Pass value

Source: Internet
Author: User

First, simple transmission value

1, modify the Mainactivity

    protected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main); //cast to Button, must be placed after Setcontentviewbtnsatrtaty1=(Button) Findviewbyid (r.id.btnstartaty1); //set the event listener for the buttonBtnsatrtaty1.setonclicklistener (//an anonymous class that implements the Onclicklistener interface            NewView.onclicklistener () {@Override Public voidOnClick (view view) {//event to start the Aty1//parameter One: examples of mainactivity//parameter two: The class of the activity to be started is defined as type class//This refers to the inner class that implements the OnclicklistenerIntent i=NewIntent (mainactivity. This, Aty1.class); I.putextra ("txt","Hello Aty1");                StartActivity (i);        }        }); System. out. Print ("onCreate"); }

2, modify Aty1 accept data and display

tvout=(TextView) Findviewbyid (r.id.tvout); Tvout.settext (Getintent (). Getstringextra("txt "));

Second, the bundle transfer value is a bit more complex data

1, modify the mainactivity through the bundle transfer value

Intent i=New Intent (mainactivity.  this, Aty1. class ); Bundle Data=new  bundle ();d ata.putstring ("","Hello Aty1"  ); I.putextras (data); startactivity (i) ;

2, modify Aty1 accept data and display

tvout=(TextView) Findviewbyid (r.id.tvout); Bundle Data=getintent (). Getextras (); String txt=data.getstring ("txt"); tvout.settext (TXT);

Second, the transfer of activity back

You cannot use startactivity instead of Startactivityforresult

1. Modify Aty1 to set the return value when the page is closed

        btnclose=(Button) Findviewbyid (r.id.btnstartaty1);        Btnclose.setonclicklistener (new View.onclicklistener () {            @Override            void onClick (view view) {Intent i=new Intent (); I.putextra ("result","Hello mainactivity"); Setresult (0, i); Finish ();}});             

2. Modify Mainactivity to accept the callback value

    protected void onactivityresult (intint  resultcode, Intent data) {        String result= Data.getstringextra ("result");        Tvout.settext (result);        Super.onactivityresult (Requestcode, ResultCode, data);    }

Android-activity use (2)-Pass value

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.