IT. Expert System.

Android Reference



Class ShareActionProvider

  • public class ShareActionProvider
    extends ActionProvider
    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:

      // In Activity#onCreateOptionsMenu
      public boolean onCreateOptionsMenu(Menu menu) {
          // Get the menu item.
          MenuItem menuItem = menu.findItem(;
          // 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 MenuItem.getActionView() calls
          // ActionProvider.onCreateActionView() which uses the backing file name. Omit this
          // line if using the default share history file is desired.
          . . .
      // Somewhere in the application.
      public void doShare(Intent shareIntent) {
          // When you want to share set the share intent.

    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.

    See Also:
    • Field Detail


        public static final String DEFAULT_SHARE_HISTORY_FILE_NAME
        The default name for storing share history.
        See Also:
        Constant Field Values
    • Constructor Detail

      • ShareActionProvider

        public ShareActionProvider(Context context)
        Creates a new instance.
        context - Context for accessing resources.
    • Method Detail

      • setOnShareTargetSelectedListener

        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.

        listener - The listener.
      • onCreateActionView

        public View onCreateActionView()
        Factory method called by the Android framework to create new action views.

        This method has been deprecated in favor of ActionProvider.onCreateActionView(MenuItem). Newer apps that wish to support platform versions prior to API 16 should also implement this method to return a valid action view.

        Specified by:
        onCreateActionView in class ActionProvider
        A new action view.
      • onPrepareSubMenu

        public void onPrepareSubMenu(SubMenu subMenu)
        Called to prepare an associated submenu for the menu item backed by this ActionProvider.

        if 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.

        onPrepareSubMenu in class ActionProvider
        subMenu - Submenu that will be displayed
      • setShareHistoryFileName

        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(). 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 setShareHistoryFileName(String) you must call Activity.invalidateOptionsMenu() to recreate the action view. You should not call Activity.invalidateOptionsMenu() from Activity.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); invalidateOptionsMenu(); }

        shareHistoryFile - The share history file name.
      • setShareIntent

        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);
          Uri uri = Uri.fromFile(new File(getFilesDir(), "foo.jpg"));
          shareIntent.putExtra(Intent.EXTRA_STREAM, uri.toString());

        shareIntent - The share intent.
        See Also:


Android Reference

Java basics

Java Enterprise Edition (EE)

Java Standard Edition (SE)





Java Script








Design patterns

RFC (standard status)

RFC (proposed standard status)

RFC (draft standard status)

RFC (informational status)

RFC (experimental status)

RFC (best current practice status)

RFC (historic status)

RFC (unknown status)

IT dictionary

All information of this service is derived from the free sources and is provided solely in the form of quotations. This service provides information and interfaces solely for the familiarization (not ownership) and under the "as is" condition.
Copyright 2016 © ELTASK.COM. All rights reserved.
Site is optimized for mobile devices.
Downloads: 145 / 158782895. Delta: 0.04104 с