ShareActionProvider does not work when showAsAction=ifRoom
Asked Answered
C

1

8

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!

Carencarena answered 29/11, 2013 at 14:56 Comment(0)
M
14

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.

Mungovan answered 29/11, 2013 at 15:11 Comment(5)
My Intent depends on user action. So I can't set it in onCreateOptionsMenu. If I set some static value, Share button is enabled. But without doing all this, it works fine when the showAsAction="never". It disables only when showAsAction="ifRoom", which looks strange to me.Carencarena
Currently, I'm calling setShareIntent in onOptionsItemSelected method. The content I share is dynamic. If I change it to ifRoom, Share button is disabled. It works when I setShareIntent in the onCreateOptionsMenu as you have suggested. I tried setting with a String member variable and tried to update the variable based on the content. It only takes the first value with which I set the Intent.Carencarena
I appended to my answer addressing your last comment. Please check it outMungovan
Cool...It makes sense now. I moved setShareIntent to another method and it works as expected now. Thanks!Carencarena
HEY @jobinbasani, can you explain how did you achieve that ?Montreal

© 2022 - 2024 — McMap. All rights reserved.