Steve D
Steve D

Reputation: 435

Successful share intent for android

How can i tell if a user successfully completed a share intent? For instance if a user wanted to share an app via Facebook or Twitter.

Edit:

I am not looking at how to create an Intent for sharing. I want to know if the user actually shared anything. Or did the user hit the cancel button.

Upvotes: 43

Views: 14978

Answers (4)

A. Kazarovets
A. Kazarovets

Reputation: 1677

Found the option, suitable for Android >= 22. Maybe it can help somebody.

Starting with Android 22 there's an option to send IntentSender object in createChooser method. You can create a pending intent for a broadcast receiver in which you can get the package name of the app on which a user clicked.

Receiver:

public class MyReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    super.onReceive(context, intent);
    // do something here
}
}

Manifest:

<receiver android:name="MyReceiver" android:exported="false"/>

Creating pending intent:

PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, receiver, PendingIntent.FLAG_UPDATE_CURRENT);

And then using it in the chooser intent:

startActivity(Intent.createChooser(share
                            , "some_title"
                            , pendingIntent.getIntentSender()));

Then in onReceiver you can get the package name of the app:

String selectedAppPackage = String.valueOf(intent.getExtras().get(EXTRA_CHOSEN_COMPONENT))

Source: medium blogpost

Upvotes: 4

Rony Tesler
Rony Tesler

Reputation: 1401

For twitter - the "data" object in OnActivityResult is null when the user cancels the share.

Upvotes: 2

xagema
xagema

Reputation: 773

You have use the Intent.ACTION_SEND, and the system will display a list of applications (on the device) where you can share. This website explains how:

http://sudarmuthu.com/blog/sharing-content-in-android-using-action_send-intent

Upvotes: -11

HRJ
HRJ

Reputation: 17767

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.

Upvotes: 25

Related Questions