在测试期间,我注意到有时我的子活动的完成()不会执行 onActivityResult。大多数时候它工作正常,我不知道何时以及为什么会出现此问题。
子活动开始:
public void launchSubActivity(Class<? extends Activity> subActivityClass, Bundle data,
OnSubActivityResult callback) {
Intent i = new Intent(this, subActivityClass);
if(data!=null) i.putExtras(data);
Random rand = new Random();
int correlationId = rand.nextInt();
_callbackMap.put(correlationId, callback);
startActivityForResult(i, correlationId);
}
子活动完成:
public void select() {
Bundle b = new Bundle();
b.putInt("YEAR", year_result);
b.putInt("MONTH", month_result);
b.putInt("DAY", day_result);
this.getIntent().putExtras(b);
this.setResult(RESULT_OK, this.getIntent());
this.finish();
}
onActivityResult(由Nazmul Idris提供):
/**
* this is the underlying implementation of the onActivityResult method that
* handles auto generation of correlationIds and adding/removing callback
* functors to handle the result
*/
@Override
protected void onActivityResult(int correlationId, int resultCode,
Intent data) {
Log.d(Prototype.TAG, "SimpleActivity Result "+resultCode);
try {
OnSubActivityResult callback = _callbackMap.get(correlationId);
switch (resultCode) {
case Activity.RESULT_CANCELED:
callback.onResultCancel(data);
_callbackMap.remove(correlationId);
break;
case Activity.RESULT_OK:
callback.onResultOkay(data);
_callbackMap.remove(correlationId);
break;
default:
Log.e(Prototype.TAG,
"Couldn't find callback handler for correlationId");
}
} catch (Exception e) {
Log
.e(Prototype.TAG,
"Problem processing result from sub-activity", e);
}
}