Search code examples
androidcontextual-action-barshareactionprovider

How to share multiple files using ShareActionProvider in Contextual Action Bar?


I am having a ListView in my Activity. On long press of that list view, I have the contextual action bar opened. It has a Share option to share the selected items in the list (multiple items can be selected) through gmail/whatsapp/etc.

<item android:id="@+id/menu_share" android:title="Share" android:icon="@android:drawable/ic_menu_share" android:actionProviderClass="android.widget.ShareActionProvider" app:showAsAction="ifRoom" />

this is my Share option in the menu layout file.

I have a class implementing ActionMode.Callback to handle the contextual action bar and its icons.

I initialized a ShareActionProvider object inside the overriden onCreateActionMode(ActionMode mode, Menu menu) for the Share menu item.

mShareActionProvider = (ShareActionProvider) menuItem.getActionProvider();

My list view consists of filenames. My requirement is that, if the user selects multiple items in the list view (first long press and then normal press for successive selection) I should be able to share all those files.

I am using the below for such an intent (all files corresponding to the filenames in the listview are audio - amr files).

Intent shareIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM,uriList);
shareIntent.setType("audio/AMR");

Here, uriList is an ArrayList of Uris. Now where should I call the method mShareActionProvider.setShareIntent(intent); ?

I can't call it in the onCreateActionMode(ActionMode mode, Menu menu) because, the user may select some more items in the list after the first long press.

If I call it directly in the onActionItemClicked(ActionMode mode, MenuItem item) , my Share icon seems to be inactive.

If I call it in both onCreateActionMode(ActionMode mode, Menu menu) and onActionItemClicked(ActionMode mode, MenuItem item) using the same intent, the share intent doesn't seem to be updated. Kindly help..


Solution

  • I can't call it in the onCreateActionMode(ActionMode mode, Menu menu) because, the user may select some more items in the list after the first long press.

    Try calling it there, but also call it again in onItemCheckedStateChanged(), when the mix of checked items changes, providing a new Intent that has the updated extras.