FileDocCategorySizeDatePackage
ShareCompat.javaAPI DocAndroid 5.1 API38762Thu Mar 12 22:22:56 GMT 2015android.support.v4.app

ShareCompat

public class ShareCompat extends Object
Extra helper functionality for sharing data between activities. ShareCompat provides functionality to extend the {@link Intent#ACTION_SEND}/ {@link Intent#ACTION_SEND_MULTIPLE} protocol and support retrieving more info about the activity that invoked a social sharing action. {@link 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 {@link IntentBuilder#from(Activity)}. {@link IntentReader} provides helper functions for parsing the defined extras within an {@link Intent#ACTION_SEND} or {@link Intent#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.

Fields Summary
public static final String
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 {@link Activity#getCallingPackage()}.) Instead of using this constant directly, consider using {@link #getCallingPackage(Activity)} or {@link IntentReader#getCallingPackage()}.
public static final String
EXTRA_CALLING_ACTIVITY
Intent extra that stores the {@link ComponentName} of the calling activity for an ACTION_SEND intent.
private static ShareCompatImpl
IMPL
Constructors Summary
Methods Summary
public static voidconfigureMenuItem(android.view.MenuItem item, android.support.v4.app.ShareCompat$IntentBuilder shareIntent)
Configure a {@link MenuItem} to act as a sharing action.

If the app is running on API level 14 or higher (Android 4.0/Ice Cream Sandwich) 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 {@link MenuItemCompat#setShowAsAction(MenuItem, int)} to request that behavior in addition to calling this method.

If the app is running on an older platform version this method will configure a standard activity chooser dialog for the menu item.

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 {@link ActivityCompat#invalidateOptionsMenu(Activity)}. If the app is running on API level 11 or above and uses the Action Bar its menu will be recreated and rebuilt. If not, the activity will receive a call to {@link Activity#onPrepareOptionsMenu(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.

param
item MenuItem to configure for sharing
param
shareIntent IntentBuilder with data about the content to share

        IMPL.configureMenuItem(item, shareIntent);
    
public static voidconfigureMenuItem(android.view.Menu menu, int menuItemId, android.support.v4.app.ShareCompat$IntentBuilder shareIntent)
Configure a menu item to act as a sharing action.

param
menu Menu containing the item to use for sharing
param
menuItemId ID of the share item within menu
param
shareIntent IntentBuilder with data about the content to share
see
#configureMenuItem(MenuItem, IntentBuilder)

        MenuItem item = menu.findItem(menuItemId);
        if (item == null) {
            throw new IllegalArgumentException("Could not find menu item with id " + menuItemId +
                    " in the supplied menu");
        }
        configureMenuItem(item, shareIntent);
    
public static android.content.ComponentNamegetCallingActivity(android.app.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.

param
calledActivity Current activity that was launched to share content
return
ComponentName of the calling activity

        ComponentName result = calledActivity.getCallingActivity();
        if (result == null) {
            result = calledActivity.getIntent().getParcelableExtra(EXTRA_CALLING_ACTIVITY);
        }
        return result;
    
public static java.lang.StringgetCallingPackage(android.app.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.

param
calledActivity Current activity that was launched to share content
return
Name of the calling package

        if (Build.VERSION.SDK_INT >= 16) {
            IMPL = new ShareCompatImplJB();
        } else if (Build.VERSION.SDK_INT >= 14) {
            IMPL = new ShareCompatImplICS();
        } else {
            IMPL = new ShareCompatImplBase();
        }
    
        String result = calledActivity.getCallingPackage();
        if (result == null) {
            result = calledActivity.getIntent().getStringExtra(EXTRA_CALLING_PACKAGE);
        }
        return result;