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