当子活动完成onActivityResult有时不叫 [英] onActivityResult sometimes not called when Sub-Activity finishes

查看:143
本文介绍了当子活动完成onActivityResult有时不叫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在测试过程中,我发现有时候我的子活动结束()不执行onActivityResult。大多数时候它的工作好了,我想不通,何时,为何出现此问题。

子活动启动:

 公共无效launchSubActivity(类和LT ;?延伸活动> subActivityClass,捆绑数据,
        OnSubActivityResult回调){

    意图I =新的意图(这一点,subActivityClass);
    如果(数据!= NULL)i.putExtras(数据);

    随机兰特=新的随机();
    INT的correlationID = rand.nextInt();

    _callbackMap.put(的correlationID,回调);

    startActivityForResult(ⅰ,的correlationID);

}
 

次级活动结束:

 公共无效选择(){
    叠B =新包();
    b.putInt(年,year_result);
    b.putInt(月,month_result);
    b.putInt(日,day_result);
    this.getIntent()putExtras(b)中。;
    this.setResult(RESULT_OK,this.getIntent());
    this.finish();
}
 

onActivityResult(由纳兹穆尔伊德里斯):

  / **
 *这是onActivityResult方法的基本实现,
 *处理自动生成correlationIds和添加/删除回调
 *仿函数来处理结果
 * /
@覆盖
保护无效onActivityResult(INT的correlationID,诠释结果code,
        意图数据){

    Log.d(Prototype.TAG,SimpleActivity结果+结果code);

    尝试 {
        OnSubActivityResult回调= _callbackMap.get(的correlationID);

        开关(结果code){
        案例Activity.RESULT_CANCELED:
            callback.onResultCancel(数据);
            _callbackMap.remove(的correlationID);
            打破;
        案例Activity.RESULT_OK:
            callback.onResultOkay(数据);
            _callbackMap.remove(的correlationID);
            打破;
        默认:
            Log.e(Prototype.TAG,
                    找不到回调处理的correlationID);
        }
    }赶上(例外五){
        登陆
                .E(Prototype.TAG,
                        从子活动问题处理结果,E);
    }

}
 

解决方案

也许你有一个像杀活动标志或限制后台进程的一些开发商的选择。

进入设置 - >开发人员选项取消选中不保留活动后台进程限制设置为标准限值

有关的额外信息,请查阅 http://stackoverflow.com/a/14195833/779408 和的http://stackoverflow.com/a/11522468/779408

During testing I noticed that sometimes the finish() of my sub-activity doesn't execute onActivityResult. Most of the times it works okay, and I can't figure out, when and why this problem occurs.

Subactivity start:

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);

}

Subactivity finish:

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 (by 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);
    }

}

解决方案

Maybe you have some developer options like kill activity flag or limit background process.

Go to setting-> developer options unchecked Don't keep activities and Background process limit set to standard limit.

For extra information check http://stackoverflow.com/a/14195833/779408 and http://stackoverflow.com/a/11522468/779408

这篇关于当子活动完成onActivityResult有时不叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆