2

i have the following code that launches a share sheet to share some data to a relivant third party app below

ShareCompat.IntentBuilder intentBuilder = ShareCompat.IntentBuilder.from(activity)
                .setChooserTitle(title)
                .setSubject(subject)
                .setText(message)
                .setType(type.typeText);

I want to implement some sort of callback so i know whether a user has selected one of the shared providers or if the user simply canceles and clicks the back key to dismiss the share sheet.

is this possible?

Jono
  • 17,341
  • 48
  • 135
  • 217

2 Answers2

2

The type of sharing which is done towards unknown applications is done using Implicit Intents. Now there is no way of knowing that any application will be launched on your Implicit Intent and to handle that we use the following check:

  if (shareIntent.resolveActivity(getPackageManager()) != null) 
  {
      startActivity(shareIntent);
  }

Now, startActivityForResult() is used in case our Implicit Intents will return any data or not, In your case you are using ShareCompat which sole function is to share the data starting it with startActiviy(Intent) or startActivityForResult(Intent) will not effect the outcome, you will not be notified of any action that is performed or not.

For more information, Please visit the following link

Abdul Rahman Shamair
  • 580
  • 1
  • 11
  • 22
  • Ok so how can i be notified if an action that was done from the share sheet activity popup? – Jono Sep 19 '19 at 08:24
  • If you use ShareActionProvider then this [answer](https://stackoverflow.com/a/23495696/5787969) will help you. (Ps. I have not tried this my self) – Abdul Rahman Shamair Sep 19 '19 at 08:39
0

Use startActivityForResult, that ways will be able to check the response. Please check: [StartActivityforResult][1]

If you need to have more control on the process, you shouldn't use the Sharing Intent approach. Instead you can use Twitter4j library for twitter and the Facebook SDK for Android for Facebook sharing and twitter if those are the apps who gets the shared data. An app with such compatibilty of callback on sharing any data with it will do so.