Share button on ActionBar appears twice

I created a share button in my action bar, but it seems to appear twice.

enter image description here

The menu XML

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 created in onCreateOptionsMenu

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 can a share button appear twice if defined, inflated, and created only once?

+3


source to share


3 answers


You inflate the menu twice, both in Control and in Fragment.



Removing inflation alone should fix the problem.

+3


source


Use menu.clear () before setting menu options;



@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
     menu.clear();
    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);
}

      

+3


source


This is due to the repeated inflation of the menu. use menu.clear () before inflation.

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

      

0


source







All Articles