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));
}
}
}
}
本文深入探讨了在Android开发中使用startActivity和startActivityForResult方法启动新活动并获取结果的机制。重点阐述了如何通过Intent描述要执行的活动,并在活动结束时通过onActivityResult方法接收返回的数据。同时,介绍了当活动失败时如何通过setResult方法返回数据到其父活动。文章旨在为Android开发者提供实用的指导,帮助他们更有效地管理应用中的活动交互。
1793

被折叠的 条评论
为什么被折叠?



