Use bundles to transfer data sources between activity activity
public class Sourceactivty extends Activity {
Private Intent Intent=null;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Intent=new Intent ();
Bundle Mybundelforname=new Bundle ();
Mybundelforname.putstring ("Key_name", "1111");
Mybundelforname.putstring ("Key_age", "22");
Intent.putextras (Mybundelforname);
TextView text= (TextView) Findviewbyid (r.id.test);
Text.setonclicklistener (New Onclicklistener () {
public void OnClick (View v) {
Intent.setclass (Sourceactivty.this, Targetactivty.class);
StartActivity (Intent);
}
});
}
}
Target Activty
public class Targetactivty extends Activity {
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (r.layout.intest);
TextView text= (TextView) Findviewbyid (R.ID.TT);
Get Data from intent
Bundle mybundelforgetname=this.getintent (). Getextras ();
String name=mybundelforgetname.getstring ("Key_name");
Text.settext ("Welcome you into:" +name);
}
}
Transfer a service request to the target activity from the source request activity through a intent
SOURCE Activty
Private Intent openwelcomeactivityintent = null;
TextView Text=null;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Openwelcomeactivityintent = new Intent ();
Set the next activity to open
text= (TextView) Findviewbyid (r.id.test);
Text.setonclicklistener (New Onclicklistener () {
public void OnClick (View v) {
Openwelcomeactivityintent.setclass (Sourceactivty1.this,
Targetactivty1.class);
Startactivityforresult (openwelcomeactivityintent, 1);
}
});
}
@Override
protected void Onactivityresult (int requestcode, int resultcode, Intent data) {
Super.onactivityresult (Requestcode, ResultCode, data);
if (Requestcode = = 1) {
if (ResultCode = = result_canceled) {
Settitle ("cancel****");
} else if (ResultCode = = RESULT_OK) {
Bundleshowbundle = Data.getextras ();//Get bundles from the returned intent
Text.settext ("The name get from the second layout:\n" + showbundle.getstring ("MyName"));
}
}
}
}
? The first parameter is the corresponding request code when you open the request intent, you can define it yourself.
? The second parameter is the validation result code returned by the target activity
? The third parameter is the intent that the target activity returns
Target Activty
public class TargetActivty1 extends Activity {
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (r.layout.intest);
Intentbackintent=new Intent ();
Bundlestringbundle=new Bundle ();
Stringbundle.putstring ("MyName", "Lishiyao");
Backintent.putextras (Stringbundle);
Setresult (RESULT_OK, backintent);//Return activity result code
Finish ();
}
}
Using bundles to pass data between activity