Share button with ShareActionProvider added twice to action bar

I have a problem with the Share button in the action bar .

Screenshot:

enter image description here

As you can see, there are two icons on the right. But I only added one icon (the first one) which is inactive. The second one is active and does what I want (sharing content). As a result, I would just like the first icon to have the behavior of the second.

Where does the second icon come from? !!

Menu:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/animation_detail_share"
        android:title="@string/share"
        android:showAsAction="ifRoom"
        android:actionProviderClass=
            "android.widget.ShareActionProvider"
        />
</menu>

      

Fragment:

@Override
public void onCreate(Bundle savedInstanceState) {
    setHasOptionsMenu(true);
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    return inflater.inflate(R.layout.fragment_inplace_animation_details, container, false);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.animation_detail_menu, menu);

    MenuItem item = menu.findItem(R.id.animation_detail_share);

    ShareActionProvider mShareActionProvider = (ShareActionProvider) item.getActionProvider();

    Intent sendIntent = new Intent();
    sendIntent.setAction(Intent.ACTION_SEND);
    sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
    sendIntent.setType("text/plain");

    if(mShareActionProvider != null) {

        mShareActionProvider.setShareIntent(Intent.createChooser(sendIntent, getResources().getText(R.string.share)));
    }
}

      

I only inflate the menu inside a fragment, not in an action.

+3


source to share


1 answer


I had two questions:

  • createChooser()

    creates a new icon
  • Have to add setShareHistoryFileName(null);

    to avoid showing another icon.


So the final code looks like this:

...
mShareActionProvider.setShareHistoryFileName(null);
mShareActionProvider.setShareIntent(sendIntent);
...

      

+1


source







All Articles