How Android studio delivers simple data between activity

Source: Internet
Author: User

1, create a new project;

2. Add a button to the activity and change the button's name and ID.

3. Create another activity, add a TextView, and set the ID. Such as:

4. Implement the following code in Mainactivity:

 Public classMainactivityextendsappcompatactivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); //Create and implement a listenerFindviewbyid (R.id.button1). Setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {//What the listener is going to doIntent i=NewIntent (mainactivity. This, Main2activity.class);//mainactivity and main2ativity are connected to allow data to pass between them.I.putextra ("Data", "Nihao");//use Putextra to transfer content to another activity, whose name is data, and the value is NihaoStartActivity (i);//start a second activity and pass I to the past            }        }); }}

5. Implement the following code in Mian2activity:

 Public classMain2activityextendsappcompatactivity {PrivateTextView TV; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (r.layout.activity_main2); Intent I=getintent ();//because Mian2activity is initiated by intend, the data associated with this activity is obtained by Getintend.tv=(TextView) Findviewbyid (R.ID.TV22); Tv.settext (I.getstringextra ("Data"));//because the name is data when passed through Putextra in mainactivity, the value is Nihao    }}

6, through the virtual machine can see the following effect:

In the first activity there is a button,

Click the button on the first one, the other activity starts, and the "Nihao" of the transfer appears

How Android studio delivers simple data between activity

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.