Search code examples
androidandroid-intentandroid-actionbarandroid-menuandroid-sharing

Share Button on ActionBar Appears Twice


I've created a share button on my Action Bar - but it seems to appear twice.

enter image description here

The menu XML file is below:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/action_share"
        android:title="@string/action_share"
        app:showAsAction="always"
        app:actionProviderClass="android.support.v7.widget.ShareActionProvider"   
    />

</menu>

And it is instantiated in the onCreateOptionsMenu in the view.

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

    MenuItem menuItem = menu.findItem(R.id.action_share);

    mShareActionProvider =
            (ShareActionProvider) MenuItemCompat.getActionProvider(menuItem);

    if(mShareActionProvider != null && !mForecastString.isEmpty()){
        mShareActionProvider.setShareIntent(createShareForecastIntent());
    } else{
        Log.d(LOG_TAG, "Share Action provider is null?");
    }

    super.onCreateOptionsMenu(menu,inflater);
}

How could the share button appear twice if it is defined, inflated, and instantiated only once?


Solution

  • You are inflating Menu twice, both in the Activity and the Fragment.

    Removing one inflation should fix the problem.