33

Can somebody tell my why the Intent data is always null?

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

if (resultCode == RESULT_OK) {
    if (requestCode == UPDATE_PROFILE_REQUEST_CODE) {

        if (data != null) {
            User user = (User) data.getExtras().getSerializable(USER_DATA_EXTRA);
            if (user != null) {
                notifyNeedUpdate(user);
            }
        } else {
            Log.e("Dev", "data is null");
        }

    }
}

}

and this is how I set the result:

setResult(RESULT_OK, getIntent().putExtra(ProfileActivity.USER_DATA_EXTRA, constructUser()));

constructUser() just creates an Object I need.

I always get Log.e("Dev", "data is null");.

Eugene
  • 59,186
  • 91
  • 226
  • 333

3 Answers3

40

Make sure that your second activity is not finished before calling

setResult(RESULT_OK, getIntent().putExtra(ProfileActivity.USER_DATA_EXTRA, constructUser()));

i.e. you should call setResult before onPause, onStop, onDestroy, finish ... etc

Nermeen
  • 15,883
  • 5
  • 59
  • 72
  • Are you sure that setResult is called before second activity is finished? – Nermeen May 10 '13 at 10:19
  • Yeah, it was an issue, the Activity is being finished before I actually was setting result. Could you Nunu please update an answer so I can accept it? `getIntent()` works fine btw, you don't need to construct a new `Intent`. Thanks! – Eugene May 10 '13 at 10:29
  • 4
    also before `onBackPressed()` – Egemen Hamutçu Dec 30 '16 at 05:28
  • I am not sure, If you never get a change to set the resul to RESULT_OK. Because you are doubting that this code may not got executed because the second activity(the one who sets the result) finished too early. Then how come in the Result receiving activity he went past the RESULT_OK code check ? Because the default result is RESULT_CANCELLED. – Jai Pandit Jul 24 '17 at 20:50
  • 1
    I need to call setResult() inside onBackPressed(). In order to make this work, I need to call setResult() before super.onBackPressed(). – Devo Mar 22 '21 at 08:10
  • Arggg, Android is so tricky!! – Ton Sep 14 '22 at 07:36
28

Posting here as a possible answer though may not be your issue exactly

Ensure your activity returning passes back something like this:

Intent returnIntent = new 
returnIntent.putExtra("result", app);
returnIntent.putExtra("element", element);
if (app.getStatus() == 2){
    returnIntent.putExtra("update", true);
    // Tell the parent that everything went okay
    setResult(Activity.RESULT_OK, returnIntent);
    Log.i(TAG, "Returning, Result Success");
} else {
    // Tell parent that nothing changed
    setResult(RESULT_CANCELED, returnIntent);
    Log.i(TAG, "Returning, Nothing changed");
}
finish();

I spent a long time suffering from null intents being returned. For me it was because in onBackPressed I was calling super.onBackPressed() before the above code. When I put it after everything worked great. If onStop/onDestroy is called too early the opportunity to pass an intent back is blocked. This may be your issue

rcbevans
  • 7,101
  • 4
  • 30
  • 46
  • + 1 helped me a lot! I had in my second activity setResult code in onStop() - this actually work to the point the onActivityResult intercepted the result in my fragment, however the intent (data) was always null, even though the requestCode matched and had result code 30. I moved the code from onStop() to onBackPressed() and bang .. I was able to get the data extras. – Mark Jun 12 '15 at 18:19
  • What is resultCode 30? – Jai Pandit Jul 24 '17 at 20:48
  • Thank you. This saved me a lot of time. – Pritam Sangani May 29 '19 at 13:02
  • This part is what I needed: "For me it was because in onBackPressed I was calling super.onBackPressed() before the above code." – sham Jul 05 '21 at 23:32
-2

You must return some data from the called Activity to calling Activity while finishing it.

URAndroid
  • 6,177
  • 6
  • 30
  • 40