ShareActionProvider does not work when showAsAction = ifRoom

I am creating a small application and trying to provide a Share button on an ActionBar. The corresponding code is as follows:

manifest

<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="18" /> 

Menu item

 <item android:id="@+id/shareMenuItem" android:showAsAction="never" android:title="@string/shareAction" android:orderInCategory="100" android:actionProviderClass="android.widget.ShareActionProvider"></item> 

activity

 public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); MenuItem shareItem = menu.findItem(R.id.shareMenuItem); mShareActionProvider = (ShareActionProvider) shareItem.getActionProvider(); return super.onCreateOptionsMenu(menu); } 

Everything works fine in this scenario. I wanted to show the Share button on an ActionBar and changed showAsAction = "ifRoom". The Share button is now displayed in the ActionBar, but is not available.

I tried changing other menu items to ifRoom and they work fine. I don’t understand why the Share button does not work correctly. Any help / suggestions are welcome!

+7
android shareactionprovider
source share
1 answer

The reason for the "share-click" button without a clickable is not enough intention for your actionProvider (this is the intention by which Android can find a match).
Try installing it through

 mShareActionProvider.setShareIntent(youIntentWithAction); 

before you return from onCreateOptionsMenu

Update
I believe that it works correctly for the case showAsAction = "never" just because the intention is set correctly by the time you open the overflow options (those marked with "never") and click " shareItem " and this does not happen when you use it action bar.
It is assumed that you configured your action in the onPrepareOptionsMenu implementation (if any), which will be called only when overflow elements are opened (+ once during startup), and not for actionBar elements.

important thing :
onOptionsItemSelected NOT triggered for a menu item using actionProvider if it is shown in the action bar (i.e. actionProvider will still run onOptionsItemSelected when the user acts if this actionProvider is in the overflow menu).

This may explain why you cannot dynamically setShareIntent for your showAsAction="ifRoom" when showAsAction="ifRoom" .

If you still want setShareIntent in onOptionsItemSelected , you may need to do this when handling the selection of another element (non-actionProvier).

Let me know if this helps.

+12
source share

All Articles