Main functions:
A main interface (main activity) can be connected to many different sub-function modules (Child Activity). After the sub-module completes the tasks, it will return to the main interface, it may also return data completed by some sub-modules to the main activity for processing. The callback function onactivityresult is required for such data exchange.
<1> startactivityforresult (IntentIntent, int requestcode );
The first parameter: an intent object
Second parameter: If> = 0,Requestcode when activity endsWill be returned in onactivityresult. To determine the activity from which the returned data is returned
<2> onactivityresult (INT requestcode, int resultcode,IntentData)
First Parameter: This integerRequestcode is providedOnactivityresult is used to determine the activity from which the returned data is returned.
This requestcode andIn startactivityforresultRequestcode corresponds.
Second Parameter: This integerResultcode is composedThe child activity is returned through its setresult () method.
Third Parameter: An intent object with returned data.
<3> setresult (Int resultcode,IntentData)
Call this method to return the data returned by the activity to the parent activity.
The first parameter:When activity endsResultcodeWill be returned in onactivityresult,Generally, it is result_canceled and result_ OK.
The second parameter:An intent object that is returned to the parent activity.
This instance has three activities. Click "fly to Mars" to jump to otheractivity. The message sent from the earth to Mars is displayed in otheractivity. Click to returnThe earth transmits messages sent from Mars to the Earth to the main activity.
Likewise:
Click "fly to the Moon" to jump to thirdactivity. The message sent from the Earth to the Moon is displayed in thirdactivity. Click to returnThe message sent from the moon to the Earth is sent to the main activity.
Specific implementation:
Main activity:
Package xiaosi. onactivityresult; </P> <p> Import android. app. activity; <br/> Import android. content. intent; <br/> Import android. OS. bundle; <br/> Import android. view. view; <br/> Import android. view. view. onclicklistener; <br/> Import android. widget. button; <br/> Import android. widget. textview; </P> <p> public class onactivityresultactivity extends activity <br/> {<br/> private buttonbutton = NULL; <br/> private Bu Ttonbutton1 = NULL; <br/> private textviewtext = NULL; <br/> Private Static final intmars = 0; <br/> Private Static final intmoon = 1; </P> <p> @ override <br/> Public void oncreate (bundle savedinstancestate) <br/>{< br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/> text = (textview) findviewbyid (R. id. text); <br/> button = (button) findviewbyid (R. id. button); <br/> button. setonclic Klistener (New onclicklistener () {<br/> @ override <br/> Public void onclick (view V) <br/>{< br/> intent = new intent (onactivityresultactivity. this, otheractivity. class); <br/> string content = "Message from Earth: Hello, I am a mouse from Earth. I really want to go to your Mars "; <br/> intent. putextra ("fromearth", content); <br/> startactivityforresult (intent, Mars); <br/>}< br/> }); <br/> button1 = (button) findviewbyid (R. id. button1); <br/> button1.setonclicklistener (New onclicklistener () {<br/> @ override <br/> Public void onclick (view V) <br/>{< br/> intent = new intent (onactivityresultactivity. this, thirdactivity. class); <br/> string content = "Message from Earth: Hello, I am from Little Mouse on Earth. I really want to go to your moon "; <br/> intent. putextra ("fromearth", content); <br/> startactivityforresult (intent, Moon); <br/>}< br/> }); <br/>}</P> <p> @ override <br/> protected void onactivityresult (INT requestcode, int resultcode, intent data) <br/>{< br/> switch (requestcode) <br/>{< br/> case Mars: <br/> bundle marsbuddle = data. getextras (); <br/> string marsmessage = marsbuddle. getstring ("frommars"); <br/> text. settext (marsmessage); <br/> break; <br/> case moon: <br/> bundle moonbuddle = data. getextras (); <br/> string moonmessage = moonbuddle. getstring ("frommoon"); <br/> text. settext (moonmessage); <br/> break; <br/>}< br/>
Otheractivity:
Package xiaosi. onactivityresult; </P> <p> Import android. app. activity; <br/> Import android. content. intent; <br/> Import android. OS. bundle; <br/> Import android. view. view; <br/> Import android. widget. button; <br/> Import android. widget. textview; </P> <p> public class otheractivity extends activity <br/>{< br/> private buttonbutton = NULL; </P> <p> @ override <br/> Public void oncreate (bundle savedinstancestate) <br/>{< br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. other); <br/> intent earthintent = getintent (); <br/> string earthmessage = earthintent. getstringextra ("fromearth"); <br/> button = (button) findviewbyid (R. id. button); <br/> button. setonclicklistener (new view. onclicklistener () {<br/> @ override <br/> Public void onclick (view v) <br/>{< br/> intent = new intent (otheractivity. this, onactivityresultactivity. class); <br/> string passstring = "Message from Mars: Hello, I am Jack from Mars. I'm glad you can come to Mars"; <br/> intent. putextra ("frommars", passstring); <br/> setresult (result_ OK, intent); <br/> finish (); <br/>}< br/> }); <br/> textview = (textview) findviewbyid (R. id. othertext); <br/> textview. settext (earthmessage); <br/>}< br/>
Thirdactivity:
Package xiaosi. onactivityresult; </P> <p> Import android. app. activity; <br/> Import android. content. intent; <br/> Import android. OS. bundle; <br/> Import android. view. view; <br/> Import android. widget. button; <br/> Import android. widget. textview; </P> <p> public class thirdactivity extends activity <br/>{< br/> private buttonbutton = NULL; </P> <p> @ override <br/> Public void oncreate (bundle savedinstancestate) <br/>{< br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. third); <br/> intent earthintent = getintent (); <br/> string earthmessage = earthintent. getstringextra ("fromearth"); <br/> button = (button) findviewbyid (R. id. button); <br/> button. setonclicklistener (new view. onclicklistener () {<br/> @ override <br/> Public void onclick (view v) <br/>{< br/> intent = new intent (thirdactivity. this, onactivityresultactivity. class); <br/> string passstring = "Lunar message: Hello, I am Lucy from the moon. Welcome to the Moon"; <br/> intent. putextra ("frommoon", passstring); <br/> setresult (result_ OK, intent); <br/> finish (); <br/>}< br/> }); <br/> textview = (textview) findviewbyid (R. id. thirdtext); <br/> textview. settext (earthmessage); <br/>}< br/>