Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onActivityResult() Intent data is always null

Tags:

android

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

like image 371
Eugene Avatar asked May 10 '13 10:05

Eugene


People also ask

What is result code in onActivityResult?

onActivityResult - resultCode is always 0.

What is onActivityResult?

onActivityResult is the callback you have on the first activity to grab the contacts you choose. Follow this answer to receive notifications.

What is the use of onActivityResult in Android?

The android startActivityForResult method, requires a result from the second activity (activity to be invoked). In such case, we need to override the onActivityResult method that is invoked automatically when second activity returns result.


1 Answers

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

like image 150
Nermeen Avatar answered Sep 19 '22 05:09

Nermeen