When activity1 enables a new activity2, a bundle object can be bound to the intent for transmission, and many types of data can be stored in the bundle object, the supported data types include int, String, arraylist, list, Boolean, byte, and Char. If activity2 is disabled and data needs to be transmitted when activity1 is returned, activity1 must start activityforresult (intent, requestcode) activate activity2 and set setresult (result_ OK, intent) in axtivity2. When activity2 is disabled, the onactivityresult () callback function of the activity is triggered ();
The complete code is as follows:
Activity1
Code
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Btnjump = (button) findviewbyid (R. Id. btnload );
Btnjump. setonclicklistener (this );
Etname = (edittext) findviewbyid (R. Id. actname );
Etpwd = (edittext) findviewbyid (R. Id. etpwd );
}
// The callback function triggered when the activity started returns the result
@ Override
Public void onactivityresult (INT requestcode, int resultcode, intent data ){
If (resultcode = result_ OK ){
Bundle bundle = data. getextras ();
Toast. maketext (this, bundle. getstring ("name") + "/" + bundle. getstring ("PWD"), Toast. length_long );
}
Super. onactivityresult (requestcode, resultcode, data );
}
@ Override
Public void onclick (view v ){
// Todo auto-generated method stub
String name = etname. gettext (). tostring ();
String Pwd = etpwd. gettext (). tostring ();
Intent intent = new intent ();
Bundle bundle = new bundle ();
Bundle. putstring ("name", name );
Bundle. putstring ("PWD", PWD );
Intent. putextras (bundle );
// Requestcode is greater than or equal to 0. When activity is returned, the onactivityresult () function is called back.
Int requestcode = 0;
// Starts an activity and the callback function is triggered when the started activity returns the result.
This. startactivityforresult (intent, requestcode );
}
Activity2:
Code
Public class twoactivity extends activity {
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
This. setcontentview (R. layout. Two );
Textview TVshow = (textview) findviewbyid (R. Id. TVshow );
Intent intent = This. getintent ();
Bundle bundle = intent. getextras ();
Final string name = bundle. getstring ("name ");
Final string Pwd = bundle. getstring ("PWD ");
TVshow. settext (name + "/" + PWD );
Button BTN = (button) findviewbyid (R. Id. btnback );
BTN. setonclicklistener (New onclicklistener (){
@ Override
Public void onclick (view v ){
// Todo auto-generated method stub
Intent DATA = new intent ();
Bundle extras = new bundle ();
Extras. putstring ("name", "Return:" + name );
Extras. putstring ("PWD", "Return:" + PWD );
Data. putextras (extras );
Twoactivity. This. setresult (result_ OK, data); // return data
Finish ();
}
});
}
}