Murugan.P Murugan.P - 1 month ago 23
Android Question

onActivityResult called before setResult

I have a program where onActivityResult() is being called with the correct request code, and a null intent parameter And, when that activity calls setResult(),onactivityresult is not called. onActivityResult() called after startActivityForResult()

public void placeListActivity(View view) {

Intent placeListIntent = new Intent(this,
PlaceListActivity.class);
// startActivity(placeListIntent);
this.startActivityForResult(placeListIntent, 5);
}

protected void onActivityResult(int request, int result, Intent arg2) {
super.onActivityResult(request, result, arg2);


if (request == 5 && result == Activity.RESULT_OK) {
handleOnActivityResult();
}


Second Activity:

@Override
public void onBackPressed() {

if(status){
setResult(Activity.RESULT_OK);
finish();
}else{
super.onBackPressed();
}




}

Answer
setResult(Activity.RESULT_OK);

You are only sending resultCode so intent will be received null. send Intent with resultCode in setResult method as a second parameter. You will get that intent in first activity.

setResult method signature

Note:- When the activity's launch mode is singleTask or singleTop, the onActivityResult will be called as soon as the activity is started with a data null. To prevent this, use Intent.setFlags(0) to reset the default flags.

Comments