0

I'm trying to implement a share intent to share images. I have a full screen activity which extends ActionBarActivity and a fragment that implements the immersivemode as the android developer's guide explains, so the user can see the image in fullscreen. The problem is that even if I can see the share icon on ActionBar, it seems that it can't be clicked, apparently because the method onOptionsItemSelected is never called.

Here's the onCreateOptionsMenu:

@Override
public boolean onCreateOptionsMenu(Menu menu) {  
    getMenuInflater().inflate(R.menu.menu_share, menu);
    return true;
}

onOptionsItemSelected:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle presses on the action bar items
    switch (item.getItemId()) {
        case R.id.menu_share:
            Intent i = getIntent();
            uriString = i.getStringExtra("uri");
            if(uriString != null) {
                Uri uri = Uri.parse(uriString);

                mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);
                mShareActionProvider.setShareIntent(createShareIntent(uri));
            }
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

Here's the method I created to prepare and start the intent:

private Intent createShareIntent(Uri uri) {
    Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
    shareIntent.setType("image/*");
    startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.share_image)));
    return shareIntent;
}

menu_share.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item android:id="@+id/menu_share"
        android:title="Share"
        app:showAsAction="ifRoom"
        android:icon="@drawable/abc_ic_menu_share_mtrl_alpha"
        app:actionProviderClass="android.support.v7.widget.ShareActionProvider"
        />
</menu>

I've tried different ways, for instance using setOnMenuItemClickListener or similar things which consider listeners, but I see that these listeners are ignored when I debug. I've also searched for a solution here and on other websites but I can't get how to solve this problem, so any idea will be appreciated. Thanks in advance!

Stark_kids
  • 507
  • 1
  • 5
  • 17

2 Answers2

2

You are inflating the wrong menu. Since your menu file is called menu.xml you should do a

getMenuInflater().inflate(R.menu.menu, menu); 

You dont directly inflate an entry, but the menu.

Also below your

switch (item.getItemId()) {
    case R.id.menu_share:

write: Log.d("mytag", "share menu clicked"); Then you can verify in your android logcat, that there is nothing wrong with the menu clicking, but with the Intent you are receiving from.

Paul Woitaschek
  • 6,717
  • 5
  • 33
  • 52
  • I'm sorry, my fault, I wrote menu.xml instead of menu_share.xml, that's the real name of my menu file. Now I edit the question, thank you!! – Stark_kids Mar 01 '15 at 17:22
  • You dont have to register any listener. I suspect that your uriString returns null. After the case R.id.menu_share: write a Toast.makeText(this, "share menu clicked", Toast.LENGTH_LONG).show(); And tell what you see. – Paul Woitaschek Mar 01 '15 at 17:41
  • Nope, the uriString returns the correct string, I've already checked it when I was trying another solution and Intent i = getIntent() and uriString = i.getStringExtra("uri"); were in onCreateOptionsMenu(Menu menu) method. The fact is that onOptionsItemSelected(MenuItem menuItem) isn't ever called...If i put a breakpoint in this method, when I click on the share icon, the debugger don't see it and nothing occurs – Stark_kids Mar 01 '15 at 17:55
  • Nope, the message doesn't pop up when I click on the share icon. – Stark_kids Mar 01 '15 at 18:02
0

Eventually, I changed my approach to solve the problem and now I'm able to call the onOptionsItemSelected(MenuItem item) method and share images from my app to the others. I put the code below.

onCreateOptionsMenu(Menu menu):

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);

    menuItem = menu.add(Menu.NONE, R.id.action_share, Menu.NONE, R.string.action_share);
    menuItem.setIcon(R.drawable.abc_ic_menu_share_mtrl_alpha);
    menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
    mShareActionProvider = new ShareActionProvider(this);
    return true;
}

onOptionsItemSelected(MenuItem item):

@Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {

            case R.id.action_share:
                Intent i = getIntent();
                uriString = i.getStringExtra("uri");

                if (uriString != null) {
                    Uri uri = Uri.parse(uriString);

                    MenuItemCompat.setActionProvider(item, mShareActionProvider);
                    createShareIntent(uri);
                    return true;
                }
            default:
                return super.onOptionsItemSelected(item);
        }
    }

My method to create the share intent when I click on the right menu item:

private void createShareIntent(Uri uri) {
    Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
    shareIntent.setType("image/*");
    if (mShareActionProvider != null) {
        mShareActionProvider.setShareIntent(shareIntent);
    }

}

The id of the item is into ids.xml in values folder:

<?xml version="1.0" encoding="utf-8"?>
    <resources>
        <item type="id" name="action_share"/>
    </resources>

strings.xml:

<!-- Actions -->
    <string name="action_share">Share image</string>

As you can see, I don't use getMenuInflater().inflate(int menuRes, Menu menu) anymore, but I use the add() method to generate my menu and it works for me.

Stark_kids
  • 507
  • 1
  • 5
  • 17