onActivityResult()有意向的数据为空后活动结束 [英] onActivityResult() has Intent data as null after an Activity has finished

查看:145
本文介绍了onActivityResult()有意向的数据为空后活动结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我打电话的startActivityForResult(),并试图处理结果在onAcvityResult()方法。但是,意图数据为空并且结果RESULT_CANCELED。我不知道为什么,虽然。

Hi there I am calling an startActivityForResult() and trying to process the result in the onAcvityResult() method. However, the Intent data is null and result is RESULT_CANCELED. I am not sure why though.

我创造与活动:

startActivityForResult(new Intent(this, Class.class),LIST_RESULT);

然后在Activity类

then in the Activity class

@Override
public void onBackPressed() {
    super.onBackPressed();

    Intent data = new Intent();
    Bundle bundle = new Bundle();

    bundle.putParcelable("name", la);
    data.putExtras(bundle);

    if (getParent() == null) {
        setResult(Activity.RESULT_OK, data);
    } else {
        getParent().setResult(Activity.RESULT_OK, data);
    }

    //finish();
}

完成()没有任何影响。其实我得到了LogCat中警告说,重复完成的请求HistoryRecord

finish() has no effect. In fact I get warning in LogCat that duplicate finish request HistoryRecord

和我处理结果:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    switch(requestCode) {
    case(LIST_RESULT):
        if(resultCode == Activity.RESULT_OK) {
            previousList = data.getExtras();
        }
    break;
    }

}

数据为空,结果code是Action.RESULT_CANCELED。

data is null and the resultCode is the Action.RESULT_CANCELED.

任何想法,为什么我没有收到任何通过?是什么改变它我之间设置它,阅读它?该mParent也是返回结果的活性空。

Any ideas why I am not getting any through? Is something changing it in between me setting it and reading it? The mParent is also null in the activity that returns result.

亚历

推荐答案

亚历克斯,

我想你要删除叫完成() onBack pressed()方法,并调用 super.onBack pressed取代它()。我相信,在调用 super.onBack pressed()呼吁结束,你永远不会得到一个机会,叫的setResult()

I think you want to remove the called to finish() in your onBackPressed() method, and replace it with the call to super.onBackPressed(). I believe the call to super.onBackPressed() is calling finish and you are never getting a chance to call setResult().

试试...

@Override
public void onBackPressed() {

    Intent data = new Intent();
    Bundle bundle = new Bundle();

    bundle.putParcelable("name", la);
    data.putExtras(bundle);

    if (getParent() == null) {
        setResult(Activity.RESULT_OK, data);
    } else {
        getParent().setResult(Activity.RESULT_OK, data);
    }

    super.onBackPressed();
}

这篇关于onActivityResult()有意向的数据为空后活动结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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