I know this question has been asked many times and i followed all the steps correctly but still getting null data in my FirstApplication
FirstApplication starting an intent to get result from a library class using Mainactivity that is initialised using the activity variable in a constructor:
sendIntent.putExtra("data", bundle);
sendIntent.setType("text/plain");
sendIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (sendIntent.resolveActivity(context.getPackageManager()) != null) {
activity.startActivityForResult(sendIntent, requestCode);
}
SecondApplication getting the intent and doing necessary operation
Intent intent = getIntent();
Bundle bundle = intent.getBundleExtra("data");
if (bundle!=null){
type = bundle.getString("type");
num1 = Integer.parseInt(bundle.getString("num1"));
num2 = Integer.parseInt(bundle.getString("num2"));
if (type.equalsIgnoreCase("add")){
result = String.valueOf(num1+num2);
}else {
result = String.valueOf(num1-num2);
}
}
Sending result to FirstApplication
Bundle bundle1 = new Bundle();
if (bundle1!=null){
Intent send = new Intent();
bundle1.putString("result", result);
send.putExtra("datasend", bundle1);
setResult(Activity.RESULT_OK, send);
finish();
}
Receiving result in FirstApplication MAinActivity
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE) {
if (resultCode == Activity.RESULT_OK) {
Bundle bundle = data.getBundleExtra("datasend");
result = bundle.getString("result");
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
But here i am receiving resultCode 0 and data null. Any help will be appreciated. Thank you.
The problem is in this line
sendIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
By calling Intent.FLAG_ACTIVITY_NEW_TASK
you are setting the second activity on the top of the back stack and cancelling the effect of startActivityForResult()
So it should be removed to get back the result