开发者

onActivityResult() has Intent data as null after an Activity has finished

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.

I am creating activity with:

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

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

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

And I am processing the result in:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data)开发者_StackOverflow中文版;

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

}

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

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.

Alex


Alex,

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().

Try...

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


I looked at the proposed solution and the original question example. In my case , the same error above was reusing the same requestCode value.

Pay attention to your requestCode on using startActivityForResult, each Activity call should have a different requestCode.

If you reuse or don’t pay attention to your requestCode to be unique for each Activity call you will get the following error message:

“onActivityResult() has Intent data as null after an Activity has finished” or ”Failure delivering result ResultInfo”.

Use variable definition for each startActivityForResult,to ensure clarity. ex.

public static final int INITIATIVE_REQUEST = 11

      Use a unique number for each of startActivityForResult.  

Again repeating the same requestCode on multiple Activities will result on the above message.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜