<pre name= "code" class= "java" >//mainactivity.javaintent intent = new intent (); Bundle bundle = new Bundle (), Bundle.putint ("Data1", "ten"), Bundle.putstring ("Data2", "data");//From current mainactivity Switch to Activity2intent.setclass (Mainactivity.this, Activity2.class); Intent.putextras (bundle);//Start switching this.startactivity (intent);
Activity2.javaintent intent= getintent (); Gets the value of the corresponding key in the extra of this intent object int data1 = Intent.getextras (). GetInt ("Data1"); String data2 = Intent.getextras (). getString ("Data2");
1.startActivity Method Jump
The preceding section of code generates a intent object and a bundle object
Intent object is the parameter that the StartActivity method uses when switching
The bundle object is a key-value pair that stores the data sent to the target Activity2 object, which can not be set
Get the intent object passed to with the Getintent method in Activity2 and get the key value pair data
2.startActivityForResult Method Jump
The previous StartActivity method can pass data from mainactivity to Activity2
If you want to get the data back after the Activity2 is finished executing mainactivity
You have to use the Startactivityforresult method.
The most common requirement for this application is to identify such interfaces as pop-up boxes
<pre code_snippet_id= "365302" snippet_file_name= "blog_20140527_6_6440008" name= "code" class= "Java" >// Mainactivity.java
Intent Intent = new Intent (); Intent.setclass (Mainactivity.this, Activity2.class);
Bundle bundle = new bundle ();
Bundle.putint ("Data1", 10);
Bundle.putstring ("Data2", "data");
Intent.putextras (bundle);
Startactivityforresult (Intent, 0);
There's only one way to change the rest, no change.
Activity2 receive key value pairs also do not need to change
Call Setresult only at the end
Activity2.javaintent intent= New Intent () Intent.putextra ("Res1", 1); Intent.putextra ("Res2", "res");// The request code can set its own setresult (RESULT_OK, intent);//Shut down the Activity2finish ();
Overriding the Onactivityresult method in Mainactivity
mainactivity.javaprotected void Onactivityresult (int requestcode, int resultcode, Intent data) {//TODO auto-generated Method Stubsuper.onactivityresult (Requestcode, ResultCode, data); switch (resultcode) {case Result_ok:string str1 = Data.getstringextra ("Res1"); String str2 = Data.getstringextra ("Res2"); Finish (); break;default:break;} Switch}//onactivityresult