当Sub-Activity完成时,有时不会调用onActivityResult

时间:2010-12-07 23:51:47

标签: android subactivity

在测试期间,我注意到有时候我的子活动的finish()没有执行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);
    }

}

2 个答案:

答案 0 :(得分:0)

也许您有一些开发人员选项,例如杀死活动标记或限制后台进程。

转到设置 - &gt;未经检查的开发者选项Don't keep activitiesBackground process limit设置为标准限制。

有关额外信息,请查看https://stackoverflow.com/a/14195833/779408https://stackoverflow.com/a/11522468/779408

答案 1 :(得分:-2)

问题是“correlationId”&lt; 0:

/** use this method to launch the sub-Activity, and provide a functor to handle the result - ok or cancel */
public void launchSubActivity(Class subActivityClass, ResultCallbackIF callback) {

  Intent i = new Intent(this, subActivityClass);
  Random rand = new Random();
  int correlationId = rand.nextInt();

/*
Values os correlationId:

1972479154

477929567

-1246508909 = NEGATIVE = INVALID! 

*/

  if (correlationId < 0)
      correlationId *= -1;

  _callbackMap.put(correlationId, callback);
  startSubActivity(i, correlationId);
}