Delivery Bundles for Android packets

Source: Internet
Author: User

Method One:

1. Create a new project and build two activity for mainactivity and main2activity, respectively

2. Mainactivity Create a button and set its text to: Start the next activity. Create a TextView in main2activity and set its ID.

3. 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 NihaoBundle b=NewBundle (); B.putstring ("Name", "Xiaohong"); B.putint ("Age", 2); I.putextras (b);//passing Bundle-packaged datastartactivity (i);//start a second activity and pass I to the past            }        }); }}

4. Implement the following code in Main2activity:

 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.Bundle Data=i.getextras ();//Get PacketsTV=(TextView) Findviewbyid (R.ID.TV22); //Tv.settext (I.getstringextra ("Data"));//because the name is data when passed through Putextra in mainactivity, the value is NihaoTv.settext (String.Format ("name=%s,age=%d", Data.getstring ("name"), Data.getint ("Age"));//show the data of the packet    }}


5, the operation effect is as follows:

In the first activity there is a button,

Click the first button, another activity starts, and the contents of the transmitted packet appear

--------------------------------------------------------------------------------------------------------------- -----------------------------------------------------------------------

Method Two:

The following code is implemented 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 NihaoBundle b=NewBundle (); B.putstring ("Name", "Xiaohong"); B.putint ("Age", 2); //I.putextras (b);//passing Bundle-packaged data//Method OneI.putextra ("Data", b);//method Two: Select a function in Putextra that can pass the bundle directlystartactivity (i);//start a second activity and pass I to the past            }        }); }}

Note: There is a function in Putextra that can pass the bundle directly:

The following code is implemented in Main2activity:

 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.//Bundle Data=i.getextras ();//method One: Get the packetBundle Data=i.getbundleextra ("data");//Method TwoTV=(TextView) Findviewbyid (R.ID.TV22); //Tv.settext (I.getstringextra ("Data"));//because the name is data when passed through Putextra in mainactivity, the value is NihaoTv.settext (String.Format ("name=%s,age=%d", Data.getstring ("name"), Data.getint ("Age"));//show the data of the packet    }}

Delivery Bundles for Android packets

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.