StartActivityForResults always returns RESULT_CANCELLED for Intent.ACTION_SEND

Siddharth picture Siddharth · May 18, 2013 · Viewed 11.9k times · Source

As the share pop up appears, I shared the content on WhatsApp successfully, but still returns RESULT_CANCELLED. Same result when I send a email using Gmail.

Calling Sharing intent, ACTION_SEND with startActivityForResult always returns CANCELLED

Intent sharingIntent = new Intent(Intent.ACTION_SEND);
    sharingIntent.setType("text/plain");
    sharingIntent.putExtra(android.content.Intent.EXTRA_TITLE, "Taxeeta, Cab Around The Curb");
    sharingIntent
        .putExtra(
            android.content.Intent.EXTRA_TEXT,
                "Hiring a cab no longer needs you to wait on call centers, or pay a"
                + " convenience (yeah right!!) charge. Taxeeta connects you"
                + " to drivers directly, for a quick book experience. With Taxeeta"
                + " you can take matters in your own hands (literally). To download"
                + " the app for your phone visit http://www.taxeeta.com");
    startActivityForResult(Intent.createChooser(sharingIntent, "Share and earn a extra Priviledge"), 111);

ActivityForResult Code

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 111) {
        if (resultCode == RESULT_OK) {
            Toast.makeText(this, "Ok DUDE", Toast.LENGTH_LONG).show();
        } else if (resultCode == RESULT_CANCELED) {
            Toast.makeText(this, "Oversmart Eh!!", Toast.LENGTH_LONG).show();
        }
    }
}

Answer

David Wasser picture David Wasser · Jun 7, 2013

startActivityForResult() only works with activities that are intended to be called that way. If the activity you are calling doesn't explicitly return a result, you will get the default result RESULT_CANCELED. Obviously ACTION_SEND is not designed to be called this way. The documentation for ACTION_SEND indicates that is generates no output (ie: generates no result).

See the documentation for Activity.startActivityForResult():

Note that this method should only be used with Intent protocols that are defined to return a result. In other protocols (such as ACTION_MAIN or ACTION_VIEW), you may not get the result when you expect. For example, if the activity you are launching uses the singleTask launch mode, it will not run in your task and thus you will immediately receive a cancel result.