Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StartActivityForResults always returns RESULT_CANCELLED for Intent.ACTION_SEND

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();
        }
    }
}
like image 782
Siddharth Avatar asked May 18 '13 07:05

Siddharth


People also ask

What is purpose of startActivityForResult () function?

By the help of android startActivityForResult() method, we can send information from one activity to another and vice-versa. The android startActivityForResult method, requires a result from the second activity (activity to be invoked).

What is request code startActivityForResult?

The request code is any int value. The request code identifies the return result when the result arrives. ( You can call startActivityForResult more than once before you get any results. When results arrive, you use the request code to distinguish one result from another.

What is the function of the requestCode when you execute the method startActivityForResult?

The requestCode helps you to identify from which Intent you came back. For example, imagine your Activity A (Main Activity) could call Activity B (Camera Request), Activity C (Audio Recording), Activity D (Select a Contact).

What can I use instead of startActivityForResult?

But recently startActivityForResult() method is deprecated in AndroidX. Android came up with ActivityResultCallback (also called Activity Results API) as an alternative for it.


1 Answers

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.

like image 126
David Wasser Avatar answered Oct 28 '22 19:10

David Wasser