ShareActionProvider

open class ShareActionProvider
kotlin.Any
   ↳ androidx.appcompat.widget.ShareActionProvider

Provides a share action, which is suitable for an activity's app bar. Creates views that enable data sharing. If the provider appears in the overflow menu, it creates a submenu with the appropriate sharing actions. Adding a share action

To add a "share" action to your activity, put a ShareActionProvider in the app bar's menu resource. For example:

 <item android:id="@+id/action_share" android:title="@string/share" app:showAsAction="ifRoom" app:actionProviderClass="androidx.appcompat.widget.ShareActionProvider"/> 

You do not need to specify an icon, since the ShareActionProvider widget takes care of its own appearance and behavior. However, you do need to specify a title with android:title, in case the action ends up in the overflow menu.

Next, set up the intent that contains the content your activity is able to share. You should create this intent in your handler for onCreateOptionsMenu(), and update it every time the shareable content changes. To set up the intent:

  1. Get a reference to the ShareActionProvider by calling and passing the share action's android.view.MenuItem. For example:
     MenuItem shareItem = menu.findItem(R.id.action_share); ShareActionProvider myShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(shareItem);
  2. Create an intent with the android.content.Intent#ACTION_SEND action, and attach the content shared by the activity. For example, the following intent shares an image:
     Intent myShareIntent = new Intent(Intent.ACTION_SEND); myShareIntent.setType("image/*"); myShareIntent.putExtra(Intent.EXTRA_STREAM, myImageUri);
  3. Call setShareIntent() to attach this intent to the action provider:
     myShareActionProvider.setShareIntent(myShareIntent); 
  4. When the content changes, modify the intent or create a new one, and call setShareIntent() again. For example:
     // Image has changed! Update the intent: myShareIntent.putExtra(Intent.EXTRA_STREAM, myNewImageUri); myShareActionProvider.setShareIntent(myShareIntent);
Share target rankings

The share action provider retains a ranking for each share target, based on how often the user chooses each one. The more often a user chooses a target, the higher its rank; the most-commonly used target appears in the app bar as the default target.

By default, the target ranking information is stored in a private file with the name specified by . Ordinarily, the share action provider stores all the history in this single file. However, using a single set of rankings may not make sense if the share action provider is used for different kinds of content. For example, if the activity sometimes shares images and sometimes shares contacts, you would want to maintain two different sets of rankings.

To set the history file, call setShareHistoryFileName() and pass the name of an XML file. The file you specify is used until the next time you call .

Summary

Nested classes
abstract

Listener for the event of selecting a share target.

Constants
static String

The default name for storing share history.

Public constructors
<init>(context: Context!)

Creates a new instance.

Public methods
open Unit
setShareIntent(shareIntent: Intent!)

Sets an intent with information about the share action.

open Boolean

open Unit

Sets a listener to be notified when a share target has been selected.

open Unit

open View!

open Unit
setShareHistoryFileName(shareHistoryFile: String!)

Sets the file name of a file for persisting the share history which history will be used for ordering share targets.

Constants

DEFAULT_SHARE_HISTORY_FILE_NAME

static val DEFAULT_SHARE_HISTORY_FILE_NAME: String

The default name for storing share history.

Value: "share_history.xml"

Public constructors

<init>

ShareActionProvider(context: Context!)

Creates a new instance.

Parameters
context Context!: Context for accessing resources.

Public methods

setShareIntent

open fun setShareIntent(shareIntent: Intent!): Unit

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.toString()); 

Parameters
shareIntent Intent!: The share intent.

hasSubMenu

open fun hasSubMenu(): Boolean

setOnShareTargetSelectedListener

open fun setOnShareTargetSelectedListener(listener: ShareActionProvider.OnShareTargetSelectedListener!): Unit

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.

Parameters
listener ShareActionProvider.OnShareTargetSelectedListener!: The listener.

onPrepareSubMenu

open fun onPrepareSubMenu(subMenu: SubMenu!): Unit

onCreateActionView

open fun onCreateActionView(): View!

setShareHistoryFileName

open fun setShareHistoryFileName(shareHistoryFile: String!): Unit

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(). Defaults to #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() 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 with #setShareHistoryFileName(String) you must call androidx.appcompat.app.AppCompatActivity#supportInvalidateOptionsMenu() to recreate the action view. You should not call androidx.appcompat.app.AppCompatActivity#supportInvalidateOptionsMenu() from androidx.appcompat.app.AppCompatActivity#onCreateOptionsMenu(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); supportInvalidateOptionsMenu(); } 
Parameters
shareHistoryFile String!: The share history file name.