# ShareCompat

Artifact: [androidx.core:core](https://developer.android.com/jetpack/androidx/releases/core) [View Source](https://cs.android.com/search?q=file:androidx/core/app/ShareCompat.java+class:androidx.core.app.ShareCompat) Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

*** ** * ** ***


```
public final class ShareCompat
```

<br />

*** ** * ** ***

Extra helper functionality for sharing data between activities. ShareCompat provides functionality to extend the `https://developer.android.com/reference/android/content/Intent.html#ACTION_SEND`/ `https://developer.android.com/reference/android/content/Intent.html#ACTION_SEND_MULTIPLE` protocol and support retrieving more info about the activity that invoked a social sharing action. `https://developer.android.com/reference/androidx/core/app/ShareCompat.IntentBuilder` provides helper functions for constructing a sharing intent that always includes data about the calling activity and app. This lets the called activity provide attribution for the app that shared content. Constructing an intent this way can be done in a method-chaining style. To obtain an IntentBuilder with info about your calling activity, use the static method `https://developer.android.com/reference/androidx/core/app/ShareCompat.IntentBuilder#from(android.app.Activity)`. `https://developer.android.com/reference/androidx/core/app/ShareCompat.IntentReader` provides helper functions for parsing the defined extras within an `https://developer.android.com/reference/android/content/Intent.html#ACTION_SEND` or `https://developer.android.com/reference/android/content/Intent.html#ACTION_SEND_MULTIPLE` intent used to launch an activity. You can also obtain a Drawable for the caller's application icon and the application's localized label (the app's human-readable name). Social apps that enable sharing content are encouraged to use this information to call out the app that the content was shared from.

## Summary

| ### Nested types |
|---|
| `public class https://developer.android.com/reference/androidx/core/app/ShareCompat.IntentBuilder` IntentBuilder is a helper for constructing `https://developer.android.com/reference/android/content/Intent.html#ACTION_SEND` and `https://developer.android.com/reference/android/content/Intent.html#ACTION_SEND_MULTIPLE` sharing intents and starting activities to share content. |
| `public class https://developer.android.com/reference/androidx/core/app/ShareCompat.IntentReader` IntentReader is a helper for reading the data contained within a sharing (ACTION_SEND) Intent. |

| ### Constants |
|---|---|
| `static final https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/androidx/core/app/ShareCompat#EXTRA_CALLING_ACTIVITY() = "androidx.core.app.EXTRA_CALLING_ACTIVITY"` Intent extra that stores the `https://developer.android.com/reference/android/content/ComponentName.html` of the calling activity for an ACTION_SEND intent. |
| `static final https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/androidx/core/app/ShareCompat#EXTRA_CALLING_ACTIVITY_INTEROP() = "android.support.v4.app.EXTRA_CALLING_ACTIVITY"` Intent extra that stores the `https://developer.android.com/reference/android/content/ComponentName.html` of the calling activity for an ACTION_SEND intent. |
| `static final https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/androidx/core/app/ShareCompat#EXTRA_CALLING_PACKAGE() = "androidx.core.app.EXTRA_CALLING_PACKAGE"` Intent extra that stores the name of the calling package for an ACTION_SEND intent. |
| `static final https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/androidx/core/app/ShareCompat#EXTRA_CALLING_PACKAGE_INTEROP() = "android.support.v4.app.EXTRA_CALLING_PACKAGE"` Intent extra that stores the name of the calling package for an ACTION_SEND intent. |

| ### Public methods |
|---|---|
| `static void` | `[configureMenuItem](https://developer.android.com/reference/androidx/core/app/ShareCompat#configureMenuItem(android.view.MenuItem,androidx.core.app.ShareCompat.IntentBuilder))( @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/view/MenuItem.html item, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/core/app/ShareCompat.IntentBuilder shareIntent )` **This method is deprecated.** Use the system sharesheet. <br /> |
| `static void` | `[configureMenuItem](https://developer.android.com/reference/androidx/core/app/ShareCompat#configureMenuItem(android.view.Menu,int,androidx.core.app.ShareCompat.IntentBuilder))( @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/view/Menu.html menu, @https://developer.android.com/reference/androidx/annotation/IdRes int menuItemId, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/core/app/ShareCompat.IntentBuilder shareIntent )` **This method is deprecated.** Use the system sharesheet. <br /> |
| `static @https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/content/ComponentName.html` | `https://developer.android.com/reference/androidx/core/app/ShareCompat#getCallingActivity(android.app.Activity)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/app/Activity.html calledActivity)` Retrieve the ComponentName of the activity that launched calledActivity from a share intent. |
| `static @https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/androidx/core/app/ShareCompat#getCallingPackage(android.app.Activity)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/app/Activity.html calledActivity)` Retrieve the name of the package that launched calledActivity from a share intent. |

## Constants

### EXTRA_CALLING_ACTIVITY

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
public static final String EXTRA_CALLING_ACTIVITY = "androidx.core.app.EXTRA_CALLING_ACTIVITY"
```

Intent extra that stores the `https://developer.android.com/reference/android/content/ComponentName.html` of the calling activity for an ACTION_SEND intent. Instead of using this constant directly, consider using `https://developer.android.com/reference/androidx/core/app/ShareCompat#getCallingPackage(android.app.Activity)` or `https://developer.android.com/reference/androidx/core/app/ShareCompat.IntentReader#getCallingPackage()`.

### EXTRA_CALLING_ACTIVITY_INTEROP

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
public static final String EXTRA_CALLING_ACTIVITY_INTEROP = "android.support.v4.app.EXTRA_CALLING_ACTIVITY"
```

Intent extra that stores the `https://developer.android.com/reference/android/content/ComponentName.html` of the calling activity for an ACTION_SEND intent. Note that this is only for interoperability between pre-1.0 AndroidX and AndroidX 1.1+ worlds. You are strongly encouraged to use `https://developer.android.com/reference/androidx/core/app/ShareCompat#getCallingPackage(android.app.Activity)` or `https://developer.android.com/reference/androidx/core/app/ShareCompat.IntentReader#getCallingPackage()`.

### EXTRA_CALLING_PACKAGE

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
public static final String EXTRA_CALLING_PACKAGE = "androidx.core.app.EXTRA_CALLING_PACKAGE"
```

Intent extra that stores the name of the calling package for an ACTION_SEND intent. When an activity is started using startActivityForResult this is redundant info. (It is also provided by `https://developer.android.com/reference/android/app/Activity.html#getCallingPackage()`.) Instead of using this constant directly, consider using `https://developer.android.com/reference/androidx/core/app/ShareCompat#getCallingPackage(android.app.Activity)` or `https://developer.android.com/reference/androidx/core/app/ShareCompat.IntentReader#getCallingPackage()`.

### EXTRA_CALLING_PACKAGE_INTEROP

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
public static final String EXTRA_CALLING_PACKAGE_INTEROP = "android.support.v4.app.EXTRA_CALLING_PACKAGE"
```

Intent extra that stores the name of the calling package for an ACTION_SEND intent. When an activity is started using startActivityForResult this is redundant info. (It is also provided by `https://developer.android.com/reference/android/app/Activity.html#getCallingPackage()`.) Note that this is only for interoperability between pre-1.0 AndroidX and AndroidX 1.1+ worlds. You are strongly encouraged to use `https://developer.android.com/reference/androidx/core/app/ShareCompat#getCallingPackage(android.app.Activity)` or `https://developer.android.com/reference/androidx/core/app/ShareCompat.IntentReader#getCallingPackage()`.

## Public methods

### configureMenuItem

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0) Deprecated in [1.5.0](https://developer.android.com/jetpack/androidx/releases/core#1.5.0)

```
public static void [configureMenuItem](https://developer.android.com/reference/androidx/core/app/ShareCompat#configureMenuItem(android.view.MenuItem,androidx.core.app.ShareCompat.IntentBuilder))(
    @NonNull MenuItem item,
    @NonNull ShareCompat.IntentBuilder shareIntent
)
```

> [!CAUTION]
> **This method is deprecated.**   
>
> Use the system sharesheet. See https://developer.android.com/training/sharing/send

Configure a `https://developer.android.com/reference/android/view/MenuItem.html` to act as a sharing action.

This method will configure a ShareActionProvider to provide a more robust UI for selecting the target of the share. History will be tracked for each calling activity in a file named with the prefix ".sharecompat_" in the application's private data directory. If the application wishes to set this MenuItem to show as an action in the Action Bar it should use `https://developer.android.com/reference/android/view/MenuItem.html#setShowAsAction(int)` to request that behavior in addition to calling this method.

During the calling activity's lifecycle, if data within the share intent must change the app should change that state in one of several ways:

- Call `https://developer.android.com/reference/androidx/core/app/ActivityCompat#invalidateOptionsMenu(android.app.Activity)`. If the app uses the Action Bar its menu will be recreated and rebuilt. If not, the activity will receive a call to `https://developer.android.com/reference/android/app/Activity.html#onPrepareOptionsMenu(android.view.Menu)` the next time the user presses the menu key to open the options menu panel. The activity can then call configureMenuItem again with a new or altered IntentBuilder to reconfigure the share menu item.
- Keep a reference to the MenuItem object for the share item once it has been created and call configureMenuItem to update the associated sharing intent as needed.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/view/MenuItem.html item` | MenuItem to configure for sharing |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/core/app/ShareCompat.IntentBuilder shareIntent` | IntentBuilder with data about the content to share |

### configureMenuItem

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0) Deprecated in [1.5.0](https://developer.android.com/jetpack/androidx/releases/core#1.5.0)

```
public static void [configureMenuItem](https://developer.android.com/reference/androidx/core/app/ShareCompat#configureMenuItem(android.view.Menu,int,androidx.core.app.ShareCompat.IntentBuilder))(
    @NonNull Menu menu,
    @IdRes int menuItemId,
    @NonNull ShareCompat.IntentBuilder shareIntent
)
```

> [!CAUTION]
> **This method is deprecated.**   
>
> Use the system sharesheet. See https://developer.android.com/training/sharing/send

Configure a menu item to act as a sharing action.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/view/Menu.html menu` | Menu containing the item to use for sharing |
| `@https://developer.android.com/reference/androidx/annotation/IdRes int menuItemId` | ID of the share item within menu |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/core/app/ShareCompat.IntentBuilder shareIntent` | IntentBuilder with data about the content to share |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/core/app/ShareCompat#configureMenuItem(android.view.MenuItem,androidx.core.app.ShareCompat.IntentBuilder)` |   |

### getCallingActivity

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
public static @Nullable ComponentName getCallingActivity(@NonNull Activity calledActivity)
```

Retrieve the ComponentName of the activity that launched calledActivity from a share intent. Apps that provide social sharing functionality can use this to provide attribution for the app that shared the content.

*Note:* This data may have been provided voluntarily by the calling application. As such it should not be trusted for accuracy in the context of security or verification.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/app/Activity.html calledActivity` | Current activity that was launched to share content |

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/content/ComponentName.html` | ComponentName of the calling activity |

### getCallingPackage

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
public static @Nullable String getCallingPackage(@NonNull Activity calledActivity)
```

Retrieve the name of the package that launched calledActivity from a share intent. Apps that provide social sharing functionality can use this to provide attribution for the app that shared the content.

*Note:* This data may have been provided voluntarily by the calling application. As such it should not be trusted for accuracy in the context of security or verification.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/app/Activity.html calledActivity` | Current activity that was launched to share content |

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/java/lang/String.html` | Name of the calling package |