http://blog.sina.com.cn/s/blog_83940dfb0100veas.html
For data transfer between activity
in the start activity, the data is sent
protected void OnCreate (Bundle saveinstancestate) {
Super.oncreate (saveinstancestate);
Setcontentview (r.layout.thisactivity);
Intent Intent = new Intent ();
Set the start activity and target activity to indicate that the data is transferred from this activity to the next activity
Intent.setclass (Thisactivity.this,targetactivity.class);
Binding Data
Intent.putextra ("username", username);//You can also bind an array
Intent.putextra ("Userpass", Userpass);
Open target Activity
StartActivity (Intent);
}
in the target activity, receive the data:
protected void OnCreate (Bundle saveinstancestate) {
Super.oncreate (saveinstancestate);
Setcontentview (r.layout.targetactivity);
Get Intent
Intent Intent = Getintent ();
Reading data
String name = Intent.getstringextra ("username");
String pass = Intent.getstringextra ("Userpass");
}
Bundles (bundles) can also be applied
in the start activity, send the data:
protected void OnCreate (Bundle saveinstancestate) {
Super.oncreate (saveinstancestate);
Setcontentview (r.layout.thisactivity);
Intent Intent = new Intent ();
Set the start activity and target activity to indicate that the data is transferred from this activity to the next activity
Intent.setclass (Thisactivity.this,targetactivity.class);
Bind multiple data at once
Bundle bundle = new bundle ();
Bundle.putstring ("username", username);
Bundle.putstring ("Userpass", Userpass);
Intent.putextras (bundle);
Open target Activity
StartActivity (Intent);
}
in the target activity, receive the data:
protected void OnCreate (Bundle saveinstancestate) {
Super.oncreate (saveinstancestate);
Setcontentview (r.layout.targetactivity);
Get Intent
Intent Intent = Getintent ();
Reading data
Bundle bundle = Intent.getextras ();
String name = bundle.getstring ("username");
String pass = bundle.getstring ("Userpass");
}
When you need to return data from the target activity to the original activity, you can use the above method to define a new Intent to pass the data, or you can use Startactivityforresult (Intent Intent, int Requestcode); method.
In the start activity, send the data:
protected void OnCreate (Bundle saveinstancestate) {
Super.oncreate (saveinstancestate);
Setcontentview (r.layout.thisactivity);
Intent Intent = new Intent ();
Set the start activity and target activity to indicate that the data is transferred from this activity to the next activity
Intent.setclass (Thisactivity.this,targetactivity.class);
Binding Data
Intent.putextra ("username", username);//You can also bind an array
Intent.putextra ("Userpass", Userpass);
Open target Activity
Startactivityforresult (intent,1);
}
//need to override Onactivityresult method
protected void Onactivityresult (int requestcode, int resultcode, Intent Intent) {
Super.onactivityresult (requestcode,resultcode,intent);
Determine if the result code is the same as the result code of the callback
if (ResultCode = = 1) {
Get backhaul data
String name = Intent.getstringextra ("name");
String pass = Intent.getstringextra ("Pass");
Working with Data
......
}
in the target activity, receive the data:
protected void OnCreate (Bundle saveinstancestate) {
Super.oncreate (saveinstancestate);
Setcontentview (r.layout.targetactivity);
Get Intent
Intent Intent = Getintent ();
Reading data
String name = Intent.getstringextra ("username");
String pass = Intent.getstringextra ("Userpass");
Get new data from EditText to name and pass
Name = Edittext1.gettext (). toString ();
pass = Edittext2.gettext (). ToString ()
Data changes, you need to pass the changed value back to the original activity
Intent.putextra ("name", name);
Intent.putextra ("Pass", pass);
Setresult (int resultcode,intent Intent) method
Setresult (1,intent);
Destroying this activity will automatically return to the previous activity when the activity is destroyed.
Finish ();
}
Intent intended for data transfer between activity