I have a simple Android app with code like this (from the Android Documentation):
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to)));
But I can't find a way to detect of the email was successfully sent (or the user cancels out). Is there a way to read an intent response?
Is there a way to read an intent response?
Not from an arbitrary activity for an arbitrary action.
The documentation for Intent
actions will tell you if there is expected output or not. So, for example, ACTION_GET_CONTENT
is documented to have output. For those Intent
actions, you use startActivityForResult()
, and part of the output will be a "result code" to let you know generally what the result was.
However:
Not every Intent
action is documented to have output. Notably, ACTION_SEND
is not documented to have output. In that case, you don't use startActivityForResult()
(but instead use startActivity()
). Even if you do use startActivityForResult()
, you have no way to know if a negative outcome means that the user cancelled out or if the other activity simply is following the documentation and did not return a result.
Some activities are buggy and fail to return results when they should.
Your definition of a successful result and the activity's definition of a successful result may differ.
I don't think there is an assured way to do it.
You could initiate the send using startActivityForResult()
and hope that the activity which handles the Intent replies with a RESULT_OK
. But you can't rely on it to work always.
AnD then you can check
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
Toast.makeText(this,"Successfully Sharing", Toast.LENGTH_SHORT).show();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With