Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); } Public voidClick (View c) {//jump to select contact ActivityIntent Intent =NewIntent ( This, Contactactivity.class);//startactivity (Intent); //The activity initiated with this API will be onactivityresult when it is destroyed.Startactivityforresult (Intent, 10); } //If an activity returns data when it is destroyed, this method is called to receive the data//Requestcode: Used to differentiate which activity the data came from//ResultCode: Used to differentiate what type of data is returned@Overrideprotected voidOnactivityresult (intRequestcode,intResultCode, Intent data) { //TODO auto-generated Method Stub Super. Onactivityresult (Requestcode, ResultCode, data); String name= Data.getstringextra ("name"); if(Requestcode = = 10) {EditText et=(EditText) Findviewbyid (r.id.et); Et.settext (name); } } }
Public classContactactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {//TODO auto-generated Method Stub Super. OnCreate (savedinstancestate); Setcontentview (r.layout.activity_contact); ListView LV=(ListView) Findviewbyid (r.id.lv); FinalString[] Objects =Newstring[]{"Very small", "Brother forced", "World class XXX", "National Service First" }; Lv.setadapter (NewArrayadapter<string> ( This, R.layout.item_listview, R.id.tv, objects)); //Click to listen to the ListView settings entryLv.setonitemclicklistener (NewOnitemclicklistener () {//when an entry is clicked, this method calls@Override Public voidOnitemclick (adapterview<?>Parent, view view,intPositionLongID) {//when the activity returns, the data is passed through the intent objectIntent data =NewIntent (); //encapsulates the data to be passed into the intent objectData.putextra ("name", objects[position]); //when the current activity is destroyed, the intent of data is passed to the activity that initiates the current activity.Setresult (1, data); //Destroy Current Activityfinish (); } }); } @Override Public voidonbackpressed () {//TODO auto-generated Method Stub Super. onbackpressed (); }}
Activity gets the return value