Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ShareActionProvider does not work when showAsAction=ifRoom

I'm creating a small application and tried to provide a Share button on the ActionBar. The relevant code looks as below:

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 the ActionBar and changed showAsAction="ifRoom". The Share button now shows up in the ActionBar, but its not clickable.

I tried to change other menu items to ifRoom, and they are working fine. Don't really understand why Share button alone is not working correctly. Any help/suggestions are appreciated!

like image 774
jobinbasani Avatar asked Nov 29 '13 14:11

jobinbasani


1 Answers

The reason of "non-clickable" shareAction button is missing intent for your actionProvider (that is an intent for which Android could find a match).
Try setting it via

mShareActionProvider.setShareIntent(youIntentWithAction);

before you return from onCreateOptionsMenu

Update
I believe it works correctly for the case showAsAction="never" only because intent is correctly set by the time you open overflow options (those marked with "never") and click you shareItem and this does not happen when you have it in the action bar.
One guess is that you set your action intent in implementation of onPrepareOptionsMenu (if you have one), which will be only called when you open overflow items (+ once during startup) and not for actionBar items.

one important thing is:
onOptionsItemSelected is NOT triggered for menuItem with actionProvider, if it is shown in the action bar (i.e. actionProvider will still trigger onOptionsItemSelected on user action if this actionProvider is in overflow menu).

That might explain why you don't have a chance to dynamically setShareIntent for your actionProvider when showAsAction="ifRoom".

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

Let me know if that helps.

like image 128
kiruwka Avatar answered Oct 12 '22 06:10

kiruwka