Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

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

    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

like image 310
Alex Avatar asked Jun 12 '11 15:06

Alex


People also ask

What is onactivityresult () method in Android?

Whether its an image Bitmap from Camera app or its a Image from the gallery, or maybe its some custom result from your some other Activity of app, Android system will call onActivityResult () method in the original requesting Activity or Fragment class.

What intent does rnappauthmodule onactivityresult call?

This intent ends up bubbling up to call RNAppAuthModule.onActivityResult in this library. The intent is set to null. to join this conversation on GitHub .

How do I get the result of an activity in Android?

Traditional Way — The onActivityResult () Method Whether its an image Bitmap from Camera app or its a Image from the gallery, or maybe its some custom result from your some other Activity of app, Android system will call onActivityResult () method in the original requesting Activity or Fragment class.

What is the difference between onactivityresult () and parseresult () method?

And the parseResult () method will behave as a proxy for onActivityResult () method and is parsing the result Intent and extracting the data from it. You can see that we don’t need to put bunch of if-else blocks in onActivityResult () method anymore.


1 Answers

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();
}
like image 178
Will Tate Avatar answered Sep 23 '22 16:09

Will Tate