4

I am having problems with the ShareActionProvider. I like to share the current url of an webview (url can be changed dynamically via JS) via a ShareActionProvider. To do this I overwrote onOptionsItemSelected to change the Intent via setShareIntent. But when I click on the ShareActionProvider and share it via a specific application, nothing happens. I debugged it and I found out that onOptionsItemSelected is not triggered. Can somebody help me out?

public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.game, menu);

    MenuItem mShareActionProviderItem = (MenuItem) menu.findItem(R.id.action_share);
    mShareActionProvider = (ShareActionProvider) mShareActionProviderItem.getActionProvider(); // is a field
    Intent sendIntent = new Intent(Intent.ACTION_SEND);
    sendIntent.setType("text/plain");
    mShareActionProvider.setShareIntent(sendIntent); //needed to be able to click on Item

    mShareActionProviderItem.getActionView().setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String url = ((GameFragment) getFragmentManager().findFragmentByTag(GAME_FRAGMENT)).getUrl();

            /*ShareButton set URL*/
            Intent sendIntent = new Intent(Intent.ACTION_SEND);
            sendIntent.setType("text/plain");
            sendIntent.putExtra(Intent.EXTRA_TEXT, String.format(getResources().getString(R.string.share_text), url));
            mShareActionProvider.setShareIntent(sendIntent);
        }
    });
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()) {
        case R.id.action_share:
            /*URL holen*/
            String url = ((GameFragment) getFragmentManager().findFragmentByTag(GAME_FRAGMENT)).getUrl();

            /*ShareButton mit URL bestücken*/
            ShareActionProvider mShareActionProvider = (ShareActionProvider) item.getActionProvider();
            Intent sendIntent = new Intent(Intent.ACTION_SEND);
            sendIntent.setType("text/plain");
            sendIntent.putExtra(Intent.EXTRA_TEXT, String.format(getResources().getString(R.string.share_text), url));
            mShareActionProvider.setShareIntent(sendIntent);
            break;
        default:
            return super.onOptionsItemSelected(item);
    }
    return true;
janwo
  • 754
  • 2
  • 8
  • 17
  • possible duplicate http://stackoverflow.com/questions/11627892/onoptionsitemselected-not-called-when-using-actionlayout-sherlockactionbar – zizoujab May 20 '13 at 19:12
  • This post did not work for me. I described the misbehavior above. Therefore my thread is NOT a duplicate. – janwo May 20 '13 at 21:12
  • The suggestion with setOnClickListener (mentioned here http://stackoverflow.com/a/11628492/1463921) is not working, because I have a ShareActionProvider-Widget and I guess it behaves different. Both, setOnClickListener and onOptionsItemSelected are not triggered when clicking on the share button. Have anybody an idea? – janwo May 21 '13 at 23:37
  • can you complete the code of `onOptionsItemSelected` method ? – zizoujab May 21 '13 at 23:56
  • I changed the code above to your needs. – janwo May 22 '13 at 14:06

1 Answers1

4

I know it's a bit late but I just had the same issue and I solved it by defining a OnShareTArgetSelectedListener on my ShareActionProvider object. :

        mShareActionProvider.setOnShareTargetSelectedListener(new OnShareTargetSelectedListener() {

            @Override
            public boolean onShareTargetSelected(ShareActionProvider arg0, Intent arg1) {

                return false;
            }
        });

http://developer.android.com/reference/android/widget/ShareActionProvider.html#setOnShareTargetSelectedListener(android.widget.ShareActionProvider.OnShareTargetSelectedListener)

Loïc
  • 353
  • 2
  • 10
  • Can you explain how did you achieve it. Because when i add it on onCreateOptionMenu, it does not work on onOptionItemSelected – Jigar Apr 26 '15 at 09:20