我有以下代码来开始活动:
public boolean onOptionsItemSelected(final MenuItem item) {
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case MENU_PREFS:
startActivityForResult(new Intent(this, PreferencesActivity.class),1);
break;
case MENU_ABOUT:
startActivityForResult(new Intent(this, AboutActivity.class),2);
break;
}
return false;
}
以及跟踪结果:
@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data){
switch (requestCode){
case 1:
case 2:
if (resultCode==RESULT_CANCELED)
finish();
}
但onActivityResult在活动(结果)开始之前触发。它很奇怪。首先,它使用resultCode = 0返回相应requestCode(在我的情况下为1或2)的结果,因此我的主要活动结束。在相应的活动(结果)真正开始之后。 WTH?
答案 0 :(得分:3)
这是Android中的已知问题或功能。请参阅涵盖类似问题的this主题
答案 1 :(得分:1)
您的活动开始的活动中的代码是什么样的?被调用活动中的标准是什么决定何时执行SetResult(Result.OK,1)以返回原始调用者?在调用Finish()或执行SetResult指令之前,您的被调用活动不会返回。
我的代码运行正常。
if (buttonSignOn.Text == "Proceed")
{
mAppFeatureMenuRetCD = "99";
Intent ProcessAppFeatureMenu = new Intent();
ProcessAppFeatureMenu.SetClass(this, typeof(AppFeatureMenu));
ProcessAppFeatureMenu.PutExtra("CallingActName", "BPM_Activity1");
ProcessAppFeatureMenu.PutExtra("AppFeatMenuStatusCode", "01");
ProcessAppFeatureMenu.PutExtra("AppFeatMenuRetCd", "00");
StartActivityForResult(ProcessAppFeatureMenu, Process_AppFeature_Menu);
return;
}
public string ReturnToCallingActivity(Intent x, string y, string z)
{
x.PutExtra("CredStatCode", strBPMCredStatCode);
x.PutExtra("RetActName", "ProgramSignOnStatus");
x.PutExtra("MiscStuff", strCallingActMiscStuff);
switch (y)
{
case "01":
SetResult(Result.Ok, x);
break;
case "02":
SetResult(Result.Canceled,x);
break;
default:
SetResult(Result.FirstUser, x);
break;
}
//Finish();
return conOK;
}