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