StartingActivitiesandGettingResults
ThestartActivity(Intent)methodisusedtostartanewactivity,(注意是new)
whichwillbeplacedatthetopoftheactivitystack.Ittakesasingleargument,anIntent,
whichdescribestheactivitytobeexecuted.
Sometimesyouwanttogetaresultbackfromanactivitywhenitends
(end这个没说清楚吧,在2.1中反正是按back键回来才调用了onActivityResult,
如果通过startActivity或startActivityForResult回来是不回调用).
Forexample,youmaystartanactivitythatletstheuserpickapersoninalistofcontacts;
whenitends,itreturnsthepersonthatwasselected.Todothis,
youcallthestartActivityForResult(Intent,int)versionwithasecondintegerparameteridentifyingthecall.
TheresultwillcomebackthroughyouronActivityResult(int,int,Intent)method.
Whenanactivityexits,itcancallsetResult(int)toreturndatabacktoitsparent.
Itmustalwayssupplyaresultcode,whichcanbethestandardresultsRESULT_CANCELED,RESULT_OK,
oranycustomvaluesstartingatRESULT_FIRST_USER.Inaddition,itcanoptionallyreturnbackanIntentcontaininganyadditionaldataitwants.Allofthisinformationappearsbackontheparent'sActivity.onActivityResult(),alongwiththeintegeridentifieritoriginallysupplied.
Ifachildactivityfailsforanyreason(suchascrashing),
theparentactivitywillreceivearesultwiththecodeRESULT_CANCELED.
publicclassMyActivityextendsActivity{
...
staticfinalintPICK_CONTACT_REQUEST=0;
protectedbooleanonKeyDown(intkeyCode,KeyEventevent){
if(keyCode==KeyEvent.KEYCODE_DPAD_CENTER){
//Whentheusercenterpresses,letthempickacontact.
startActivityForResult(
newIntent(Intent.ACTION_PICK,
newUri("content://contacts")),
PICK_CONTACT_REQUEST);
returntrue;
}
returnfalse;
}
protectedvoidonActivityResult(intrequestCode,intresultCode,
Intentdata){
if(requestCode==PICK_CONTACT_REQUEST){
if(resultCode==RESULT_OK){
//Acontactwaspicked.Herewewilljustdisplayit
//totheuser.
startActivity(newIntent(Intent.ACTION_VIEW,data));
}
}
}
}
ThestartActivity(Intent)methodisusedtostartanewactivity,(注意是new)
whichwillbeplacedatthetopoftheactivitystack.Ittakesasingleargument,anIntent,
whichdescribestheactivitytobeexecuted.
Sometimesyouwanttogetaresultbackfromanactivitywhenitends
(end这个没说清楚吧,在2.1中反正是按back键回来才调用了onActivityResult,
如果通过startActivity或startActivityForResult回来是不回调用).
Forexample,youmaystartanactivitythatletstheuserpickapersoninalistofcontacts;
whenitends,itreturnsthepersonthatwasselected.Todothis,
youcallthestartActivityForResult(Intent,int)versionwithasecondintegerparameteridentifyingthecall.
TheresultwillcomebackthroughyouronActivityResult(int,int,Intent)method.
Whenanactivityexits,itcancallsetResult(int)toreturndatabacktoitsparent.
Itmustalwayssupplyaresultcode,whichcanbethestandardresultsRESULT_CANCELED,RESULT_OK,
oranycustomvaluesstartingatRESULT_FIRST_USER.Inaddition,itcanoptionallyreturnbackanIntentcontaininganyadditionaldataitwants.Allofthisinformationappearsbackontheparent'sActivity.onActivityResult(),alongwiththeintegeridentifieritoriginallysupplied.
Ifachildactivityfailsforanyreason(suchascrashing),
theparentactivitywillreceivearesultwiththecodeRESULT_CANCELED.
publicclassMyActivityextendsActivity{
...
staticfinalintPICK_CONTACT_REQUEST=0;
protectedbooleanonKeyDown(intkeyCode,KeyEventevent){
if(keyCode==KeyEvent.KEYCODE_DPAD_CENTER){
//Whentheusercenterpresses,letthempickacontact.
startActivityForResult(
newIntent(Intent.ACTION_PICK,
newUri("content://contacts")),
PICK_CONTACT_REQUEST);
returntrue;
}
returnfalse;
}
protectedvoidonActivityResult(intrequestCode,intresultCode,
Intentdata){
if(requestCode==PICK_CONTACT_REQUEST){
if(resultCode==RESULT_OK){
//Acontactwaspicked.Herewewilljustdisplayit
//totheuser.
startActivity(newIntent(Intent.ACTION_VIEW,data));
}
}
}
}