# ShareActionProvider

Added in[API level 14](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  
Summary:[Nested Classes](https://developer.android.com/reference/android/widget/ShareActionProvider#nestedclasses)\|[Constants](https://developer.android.com/reference/android/widget/ShareActionProvider#constants)\|[Ctors](https://developer.android.com/reference/android/widget/ShareActionProvider#pubctors)\|[Methods](https://developer.android.com/reference/android/widget/ShareActionProvider#pubmethods)\|[Inherited Methods](https://developer.android.com/reference/android/widget/ShareActionProvider#inhmethods)  

# ShareActionProvider

*** ** * ** ***

[Kotlin](https://developer.android.com/reference/kotlin/android/widget/ShareActionProvider "View this page in Kotlin")\|Java

`
public


class
ShareActionProvider
`  
`


``

extends `[ActionProvider](https://developer.android.com/reference/android/view/ActionProvider)`


``


`

|---|---|------------------------------------|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) |||
| ↳ | [android.view.ActionProvider](https://developer.android.com/reference/android/view/ActionProvider) ||
|   | ↳ | android.widget.ShareActionProvider |

<br />

*** ** * ** ***

This is a provider for a share action. It is responsible for creating views that enable data sharing and also to show a sub menu with sharing activities if the hosting item is placed on the overflow menu.

Here is how to use the action provider with custom backing file in a[MenuItem](https://developer.android.com/reference/android/view/MenuItem):  

     // In Activity#onCreateOptionsMenu
     public boolean onCreateOptionsMenu(Menu menu) {
         // Get the menu item.
         MenuItem menuItem = menu.findItem(R.id.my_menu_item);
         // Get the provider and hold onto it to set/change the share intent.
         mShareActionProvider = (ShareActionProvider) menuItem.getActionProvider();
         // Set history different from the default before getting the action
         // view since a call to https://developer.android.com/reference/android/view/MenuItem#getActionView() calls
         // https://developer.android.com/reference/android/view/ActionProvider#onCreateActionView() which uses the backing file name. Omit this
         // line if using the default share history file is desired.
         mShareActionProvider.setShareHistoryFileName("custom_share_history.xml");
         . . .
     }

     // Somewhere in the application.
     public void doShare(Intent shareIntent) {
         // When you want to share set the share intent.
         mShareActionProvider.setShareIntent(shareIntent);
     }

**Note:**While the sample snippet demonstrates how to use this provider in the context of a menu item, the use of the provider is not limited to menu items.

<br />

**See also:**

- [ActionProvider](https://developer.android.com/reference/android/view/ActionProvider)

## Summary

|                                                                                                       ### Nested classes                                                                                                        ||
|--------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` interface` | [ShareActionProvider.OnShareTargetSelectedListener](https://developer.android.com/reference/android/widget/ShareActionProvider.OnShareTargetSelectedListener) Listener for the event of selecting a share target. |

|                                                                                                                         ### Constants                                                                                                                         ||
|--------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [String](https://developer.android.com/reference/java/lang/String) | [DEFAULT_SHARE_HISTORY_FILE_NAME](https://developer.android.com/reference/android/widget/ShareActionProvider#DEFAULT_SHARE_HISTORY_FILE_NAME) The default name for storing share history. |

|                                                                                                                        ### Public constructors                                                                                                                        ||
|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---|
| ` `[ShareActionProvider](https://developer.android.com/reference/android/widget/ShareActionProvider#ShareActionProvider(android.content.Context))`(`[Context](https://developer.android.com/reference/android/content/Context)` context) ` Creates a new instance. |

|                                                                                                                                                                                                                                                              ### Public methods                                                                                                                                                                                                                                                               ||
|----------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` boolean`                                                           | ` `[hasSubMenu](https://developer.android.com/reference/android/widget/ShareActionProvider#hasSubMenu())`() ` Determines if this ActionProvider has a submenu associated with it.                                                                                                                                                                                                                                                                                       |
| ` `[View](https://developer.android.com/reference/android/view/View) | ` `[onCreateActionView](https://developer.android.com/reference/android/widget/ShareActionProvider#onCreateActionView())`() ` *This method is deprecated. use[onCreateActionView(android.view.MenuItem)](https://developer.android.com/reference/android/view/ActionProvider#onCreateActionView(android.view.MenuItem))*                                                                                                                                                |
| ` void`                                                              | ` `[onPrepareSubMenu](https://developer.android.com/reference/android/widget/ShareActionProvider#onPrepareSubMenu(android.view.SubMenu))`(`[SubMenu](https://developer.android.com/reference/android/view/SubMenu)` subMenu) ` Called to prepare an associated submenu for the menu item backed by this ActionProvider.                                                                                                                                                 |
| ` void`                                                              | ` `[setOnShareTargetSelectedListener](https://developer.android.com/reference/android/widget/ShareActionProvider#setOnShareTargetSelectedListener(android.widget.ShareActionProvider.OnShareTargetSelectedListener))`(`[ShareActionProvider.OnShareTargetSelectedListener](https://developer.android.com/reference/android/widget/ShareActionProvider.OnShareTargetSelectedListener)` listener) ` Sets a listener to be notified when a share target has been selected. |
| ` void`                                                              | ` `[setShareHistoryFileName](https://developer.android.com/reference/android/widget/ShareActionProvider#setShareHistoryFileName(java.lang.String))`(`[String](https://developer.android.com/reference/java/lang/String)` shareHistoryFile) ` Sets the file name of a file for persisting the share history which history will be used for ordering share targets.                                                                                                       |
| ` void`                                                              | ` `[setShareIntent](https://developer.android.com/reference/android/widget/ShareActionProvider#setShareIntent(android.content.Intent))`(`[Intent](https://developer.android.com/reference/android/content/Intent)` shareIntent) ` Sets an intent with information about the share action.                                                                                                                                                                               |

| ### Inherited methods |
|-----------------------|---|
| From class` `[android.view.ActionProvider](https://developer.android.com/reference/android/view/ActionProvider)` ` |-------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` boolean`                                                                    | ` `[hasSubMenu](https://developer.android.com/reference/android/view/ActionProvider#hasSubMenu())`() ` Determines if this ActionProvider has a submenu associated with it.                                                                                                                                                                                                                                                                                | | ` boolean`                                                                    | ` `[isVisible](https://developer.android.com/reference/android/view/ActionProvider#isVisible())`() ` If[overridesItemVisibility()](https://developer.android.com/reference/android/view/ActionProvider#overridesItemVisibility())returns true, the return value of this method will help determine the visibility of the[MenuItem](https://developer.android.com/reference/android/view/MenuItem)this ActionProvider is bound to.                         | | ` abstract `[View](https://developer.android.com/reference/android/view/View) | ` `[onCreateActionView](https://developer.android.com/reference/android/view/ActionProvider#onCreateActionView())`() ` *This method was deprecated in API level 16. use[onCreateActionView(android.view.MenuItem)](https://developer.android.com/reference/android/view/ActionProvider#onCreateActionView(android.view.MenuItem))*                                                                                                                        | | ` `[View](https://developer.android.com/reference/android/view/View)          | ` `[onCreateActionView](https://developer.android.com/reference/android/view/ActionProvider#onCreateActionView(android.view.MenuItem))`(`[MenuItem](https://developer.android.com/reference/android/view/MenuItem)` forItem) ` Factory method called by the Android framework to create new action views.                                                                                                                                                 | | ` boolean`                                                                    | ` `[onPerformDefaultAction](https://developer.android.com/reference/android/view/ActionProvider#onPerformDefaultAction())`() ` Performs an optional default action.                                                                                                                                                                                                                                                                                       | | ` void`                                                                       | ` `[onPrepareSubMenu](https://developer.android.com/reference/android/view/ActionProvider#onPrepareSubMenu(android.view.SubMenu))`(`[SubMenu](https://developer.android.com/reference/android/view/SubMenu)` subMenu) ` Called to prepare an associated submenu for the menu item backed by this ActionProvider.                                                                                                                                          | | ` boolean`                                                                    | ` `[overridesItemVisibility](https://developer.android.com/reference/android/view/ActionProvider#overridesItemVisibility())`() ` The result of this method determines whether or not[isVisible()](https://developer.android.com/reference/android/view/ActionProvider#isVisible())will be used by the[MenuItem](https://developer.android.com/reference/android/view/MenuItem)this ActionProvider is bound to help determine its visibility.              | | ` void`                                                                       | ` `[refreshVisibility](https://developer.android.com/reference/android/view/ActionProvider#refreshVisibility())`() ` If this ActionProvider is associated with an item in a menu, refresh the visibility of the item based on[overridesItemVisibility()](https://developer.android.com/reference/android/view/ActionProvider#overridesItemVisibility())and[isVisible()](https://developer.android.com/reference/android/view/ActionProvider#isVisible()). | | ` void`                                                                       | ` `[setVisibilityListener](https://developer.android.com/reference/android/view/ActionProvider#setVisibilityListener(android.view.ActionProvider.VisibilityListener))`(`[ActionProvider.VisibilityListener](https://developer.android.com/reference/android/view/ActionProvider.VisibilityListener)` listener) ` Set a listener to be notified when this ActionProvider's overridden visibility changes.                                                  | ||
| From class` `[java.lang.Object](https://developer.android.com/reference/java/lang/Object)` ` |--------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` `[Object](https://developer.android.com/reference/java/lang/Object)          | ` `[clone](https://developer.android.com/reference/java/lang/Object#clone())`() ` Creates and returns a copy of this object.                                                                                                                                                      | | ` boolean`                                                                     | ` `[equals](https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object))`(`[Object](https://developer.android.com/reference/java/lang/Object)` obj) ` Indicates whether some other object is "equal to" this one.                                           | | ` void`                                                                        | ` `[finalize](https://developer.android.com/reference/java/lang/Object#finalize())`() ` Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.                                                          | | ` final `[Class](https://developer.android.com/reference/java/lang/Class)`<?>` | ` `[getClass](https://developer.android.com/reference/java/lang/Object#getClass())`() ` Returns the runtime class of this`Object`.                                                                                                                                                | | ` int`                                                                         | ` `[hashCode](https://developer.android.com/reference/java/lang/Object#hashCode())`() ` Returns a hash code value for the object.                                                                                                                                                 | | ` final void`                                                                  | ` `[notify](https://developer.android.com/reference/java/lang/Object#notify())`() ` Wakes up a single thread that is waiting on this object's monitor.                                                                                                                            | | ` final void`                                                                  | ` `[notifyAll](https://developer.android.com/reference/java/lang/Object#notifyAll())`() ` Wakes up all threads that are waiting on this object's monitor.                                                                                                                         | | ` `[String](https://developer.android.com/reference/java/lang/String)          | ` `[toString](https://developer.android.com/reference/java/lang/Object#toString())`() ` Returns a string representation of the object.                                                                                                                                            | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait(long,%20int))`(long timeoutMillis, int nanos) ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*, or until a certain amount of real time has elapsed. | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait(long))`(long timeoutMillis) ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*, or until a certain amount of real time has elapsed.                   | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait())`() ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*.                                                                                             | ||

## Constants

### DEFAULT_SHARE_HISTORY_FILE_NAME

Added in[API level 14](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public static final String DEFAULT_SHARE_HISTORY_FILE_NAME
```

The default name for storing share history.

<br />

Constant Value: "share_history.xml"

## Public constructors

### ShareActionProvider

Added in[API level 14](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public ShareActionProvider (Context context)
```

Creates a new instance.

<br />

|                          Parameters                           ||
|-----------|----------------------------------------------------|
| `context` | `Context`: Context for accessing resources. <br /> |

## Public methods

### hasSubMenu

Added in[API level 14](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public boolean hasSubMenu ()
```

Determines if this ActionProvider has a submenu associated with it.

Associated submenus will be shown when an action view is not. This provider instance will receive a call to[onPrepareSubMenu(android.view.SubMenu)](https://developer.android.com/reference/android/view/ActionProvider#onPrepareSubMenu(android.view.SubMenu))after the call to[onPerformDefaultAction()](https://developer.android.com/reference/android/view/ActionProvider#onPerformDefaultAction())and before a submenu is displayed to the user.

<br />

|                                           Returns                                            ||
|-----------|-----------------------------------------------------------------------------------|
| `boolean` | true if the item backed by this provider should have an associated submenu <br /> |

### onCreateActionView

Added in[API level 14](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public View onCreateActionView ()
```

<br />

**This method is deprecated.**   
use[onCreateActionView(android.view.MenuItem)](https://developer.android.com/reference/android/view/ActionProvider#onCreateActionView(android.view.MenuItem))

Factory method called by the Android framework to create new action views.

This method has been deprecated in favor of[onCreateActionView(android.view.MenuItem)](https://developer.android.com/reference/android/view/ActionProvider#onCreateActionView(android.view.MenuItem)). Newer apps that wish to support platform versions prior to API 16 should also implement this method to return a valid action view.

<br />

<br />

|                                                         Returns                                                          ||
|-------------------------------------------------------------------|-------------------------------------------------------|
| [View](https://developer.android.com/reference/android/view/View) | A new action view. This value cannot be`null`. <br /> |

### onPrepareSubMenu

Added in[API level 14](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public void onPrepareSubMenu (SubMenu subMenu)
```

Called to prepare an associated submenu for the menu item backed by this ActionProvider.

if[hasSubMenu()](https://developer.android.com/reference/android/view/ActionProvider#hasSubMenu())returns true, this method will be called when the menu item is selected to prepare the submenu for presentation to the user. Apps may use this to create or alter submenu content right before display.

<br />

|                                       Parameters                                        ||
|-----------|------------------------------------------------------------------------------|
| `subMenu` | `SubMenu`: Submenu that will be displayed This value cannot be`null`. <br /> |

### setOnShareTargetSelectedListener

Added in[API level 14](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public void setOnShareTargetSelectedListener (ShareActionProvider.OnShareTargetSelectedListener listener)
```

Sets a listener to be notified when a share target has been selected. The listener can optionally decide to handle the selection and not rely on the default behavior which is to launch the activity.

**Note:**If you choose the backing share history file you will still be notified in this callback.

<br />

<br />

|                                      Parameters                                       ||
|------------|---------------------------------------------------------------------------|
| `listener` | `ShareActionProvider.OnShareTargetSelectedListener`: The listener. <br /> |

### setShareHistoryFileName

Added in[API level 14](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public void setShareHistoryFileName (String shareHistoryFile)
```

Sets the file name of a file for persisting the share history which history will be used for ordering share targets. This file will be used for all view created by[onCreateActionView()](https://developer.android.com/reference/android/widget/ShareActionProvider#onCreateActionView()). Defaults to[DEFAULT_SHARE_HISTORY_FILE_NAME](https://developer.android.com/reference/android/widget/ShareActionProvider#DEFAULT_SHARE_HISTORY_FILE_NAME). Set to`null`if share history should not be persisted between sessions.

**Note:** The history file name can be set any time, however only the action views created by[onCreateActionView()](https://developer.android.com/reference/android/widget/ShareActionProvider#onCreateActionView())after setting the file name will be backed by the provided file. Therefore, if you want to use different history files for sharing specific types of content, every time you change the history file[setShareHistoryFileName(java.lang.String)](https://developer.android.com/reference/android/widget/ShareActionProvider#setShareHistoryFileName(java.lang.String))you must call[Activity.invalidateOptionsMenu()](https://developer.android.com/reference/android/app/Activity#invalidateOptionsMenu())to recreate the action view. You should**not** call[Activity.invalidateOptionsMenu()](https://developer.android.com/reference/android/app/Activity#invalidateOptionsMenu())from[Activity.onCreateOptionsMenu(Menu)](https://developer.android.com/reference/android/app/Activity#onCreateOptionsMenu(android.view.Menu)).  

```
 private void doShare(Intent intent) {
     if (IMAGE.equals(intent.getMimeType())) {
         mShareActionProvider.setHistoryFileName(SHARE_IMAGE_HISTORY_FILE_NAME);
     } else if (TEXT.equals(intent.getMimeType())) {
         mShareActionProvider.setHistoryFileName(SHARE_TEXT_HISTORY_FILE_NAME);
     }
     mShareActionProvider.setIntent(intent);
     invalidateOptionsMenu();
 }
```

<br />

<br />

|                            Parameters                             ||
|--------------------|-----------------------------------------------|
| `shareHistoryFile` | `String`: The share history file name. <br /> |

### setShareIntent

Added in[API level 14](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public void setShareIntent (Intent shareIntent)
```

Sets an intent with information about the share action. Here is a sample for constructing a share intent:  

```
 Intent shareIntent = new Intent(Intent.ACTION_SEND);
 shareIntent.setType("image/*");
 Uri uri = Uri.fromFile(new File(getFilesDir(), "foo.jpg"));
 shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
```

<br />

<br />

|                    Parameters                     ||
|---------------|------------------------------------|
| `shareIntent` | `Intent`: The share intent. <br /> |

**See also:**

- [Intent.ACTION_SEND](https://developer.android.com/reference/android/content/Intent#ACTION_SEND)
- [Intent.ACTION_SEND_MULTIPLE](https://developer.android.com/reference/android/content/Intent#ACTION_SEND_MULTIPLE)